this pointer

Started by
1 comment, last by Sharlin 17 years, 5 months ago
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);             
}                         
} 
 
Sylvarant @www.gamedesign.be
Advertisement
The this pointer may not be defined or assigned to: it is a read-only language keyword available by default in all member functions.
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);

This topic is closed to new replies.

Advertisement