Page 1 of 1

<SOLVED> Inheritance and Pointers

Posted: Mon Mar 01, 2010 3:10 am
by Maevik
I'm and encountering an exception anytime I try to access an SDL_Surface* from a derived class. I have a console class:

Code: Select all

class Console: public Object
{      ////////////Only displaying members which may be relevant///////////////////
	SDL_Surface* Frame;
	vector<SDL_Surface*> textLines;
	TTF_Font* font;

public:
	Console(void);
	void buildFrame();
	SDL_Surface* getImage();
};
void Console::buildFrame()  //Called by constructor.  Made it a function so I could use it later when, say, the resolution was changed
{
	SDL_Rect temp;
	Uint32 color;

	// Build the background
	temp.h = SDL_GetVideoSurface()->h;
	temp.w = SDL_GetVideoSurface()->w;
	temp.x = 0;
	temp.w = 0;
	color = SDL_MapRGB( Frame->format , CONSOLE_BACKGROUND.r , CONSOLE_BACKGROUND.g , CONSOLE_BACKGROUND.b );
	SDL_FillRect( Frame , &temp , color );

	// Build the outline
	temp.w = 4;
	color = SDL_MapRGB( Frame->format , CONSOLE_FRAME.r , CONSOLE_FRAME.g , CONSOLE_FRAME.b );
	SDL_FillRect( Frame , &temp , color );
	
	temp.x = Frame->w - temp.w;
	SDL_FillRect( Frame , &temp , color );

	temp.w = Frame->w;
	temp.x = 0;
	temp.h = 4;
	temp.y = Frame->h - 4 - 4 - 14;
	SDL_FillRect( Frame , &temp , color );

	temp.y = Frame->h - 4;
	SDL_FillRect( Frame , &temp , color );

	Object::image = Frame;
	Object::rect.x = 0;
	Object::rect.y = 0;
	Object::rect.w = Frame->w;
	Object::rect.h = Frame->h;
	
}
SDL_Surface* Console::getImage()
{
	if( Frame != NULL )
		return Frame;
	else
		return NULL;
}
And then I have the base class Object:

Code: Select all

class Object
{
	string imageName;
	bool solid;
	SDL_Rect collisionRect;

protected:
	SDL_Rect rect;
	SDL_Surface* image;			/*	Only used for Objects who cannot pre load their
									images, IE: The Console							*/

public:
	Object(void);
	Object( int x , int y , const string& imageName = "" , int w = 0 , int h = 0 );
	Object( int x , int y , int w = 0 , int h = 0 );
	~Object(void);
	void calculateMidpoint();
	string* getImageName();
	void setImageName( const string& newImageName );
	SDL_Rect* getRect() ;
	SDL_Surface* getSurface() ;
};
Basically, anytime my program tries to access myConsoleInstance->Frame , myConsoleInstance->image , myConsoleInstance->getRect()->x or anything like that I get an exception. I'm not sure what I'm doing wrong. Any help would be appreciated :D

Re: Inheritance and Pointers

Posted: Mon Mar 01, 2010 4:24 am
by short
Well..... this may help... but you can make your own decisions about whether or not making your classes friends is a good idea:
(note: I have not tested this, I'm to tired atm to even tell you if they both have to be friends of each other or just one to the other. Good luck, maybe in the morning I'll be able to help more :)
Maevik wrote:

Code: Select all

class Console: public Object
{      ////////////Only displaying members which may be relevant///////////////////
	SDL_Surface* Frame;
	vector<SDL_Surface*> textLines;
	TTF_Font* font;

public:
	Console(void);
	void buildFrame();
	SDL_Surface* getImage();
        friend class Object;
                   .....
And then I have the base class Object:

Code: Select all

class Object
{
	string imageName;
	bool solid;
	SDL_Rect collisionRect;
        friend class Console;
                ..........
There is a much better solution but atm I'm to tired, sorry :(

Re: Inheritance and Pointers

Posted: Mon Mar 01, 2010 1:14 pm
by Maevik
Ok, this seems to not be an issue with what I thought it was at all. Apparently SDL didn't like me creating FillRects on empty/null surfaces. I guess you can only draw them to the screen/main surface? Hooray for SDL_GetVideoSurface()

Re: <SOLVED> Inheritance and Pointers

Posted: Mon Mar 01, 2010 1:18 pm
by short
lol the simplest solutions are the best :)