Sign in to follow this  
Sylvarant

this pointer

Recommended Posts

my idea was to have a declaration like Font *newfont = new Font("Tahoma"); and then newfont would be inserted in the parameter that asks for the type font to print the text in. But I keep getting an error on the this pointer wich I don't seem to understand very well. font.h
 
#include<allegro.h>

class Font { 
 public: 
  Font(char*fontname); 
 ~Font();   
private :         
int numfont;
char*extension;
};       
font.cpp
 
#include "Font.h"
#include <string>
using namespace std;

Font::Font(char*fontname) { 
   numfont++;
   extension = ".pcx";
   char*loadshit = strcat(fontname, extension);
   FONT * this;
   PALETTE palette;
   this = load_font(loadshit,palette, NULL); 
   if(!fontname){ 
     allegro_message("failed to load font%s",allegro_error);             
}                         
} 
 

Share this post


Link to post
Share on other sites
The this pointer may not be defined or assigned to: it is a read-only language keyword available by default in all member functions.

Share this post


Link to post
Share on other sites
Also:

// This does nothing good; numfont just contains some garbage value
// as it has not been initialized, and every Font object will have
// their own garbage numfont.
// If you want to keep book of the number of Font objects created,
// numfont should be declared static and defined in the .cpp like this:
// int Font::numfont = 0;
numfont++;

// This shouldn't even compile; extension is a char*, but string literals
// (such as ".pcx") are of type const char*.
extension = ".pcx";

// In the best case, this will probably crash your program.
// In the worst case, it will just cause some erratic behaviour.
// strcat assumes that there is enough room in fontname for extension,
// but there very probably won't be -> it happily writes outside
// the bounds of fontname, with unfortunate consequences.
// Use the std::string!
char*loadshit = strcat(fontname, extension);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this