Jump to content
  • Advertisement
Sign in to follow this  
Sylvarant

this pointer

This topic is 4349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!