I'm trying to create a 2D game using the SDL Library and I thought it would be a great chance to practice object orientated programming and polymorphism. I'm having a few problems when it comes to inheritance however.
Here is the code for my game entities, I have one base object which is what game entities(players, moving/animating objects) are derived from as well as a UIBase which is derived from the base object. From this UIBase other objects relating to UI (buttons, menus, etc..) are derived from.
From what I understood about inheritance any class I derive from the UIBase should have access to the methods and members of both the UIBase class and the base object class, but this doesn't seem to be the case... Is there any way to ensure that and classes that I derive from UIBase have the same functions/members as the base object class?
#ifndef _ENTITIES_H_
#define _ENTITIES_H_
#include "Constants.h"
class BaseGameObject
{
protected:
SDL_Rect Location;
SDL_Surface* Image;
public:
BaseGameObject();
BaseGameObject(std::string Filename, unsignedint X = 0, unsignedint Y = 0);
virtual ~BaseGameObject();
virtualvoid LoadImage(std::string Filename, unsignedint X = 0, unsignedint Y = 0);
virtualvoid Render();
};
class BounceObject : public BaseGameObject
{
private:
int YDir;
int XDir;
public:
BounceObject();
BounceObject(std::string Filename, unsignedint X = 0, unsignedint Y = 0);
virtualvoid Render();
virtual ~BounceObject();
};
class UIGameObject : public BaseGameObject
{
protected:
SDL_Rect Collider;
public:
UIGameObject(std::string Filename, unsignedint X, unsignedint Y);
UIGameObject();
~UIGameObject();
virtualvoid OnLoop() = 0;
virtualvoid OnEvent(SDL_Event* Event) = 0;
};
class PushButton : public UIGameObject
{
public:
PushButton();
PushButton(std::string Filename, unsignedint X = 0, unsignedint Y = 0);
virtualvoid OnLoop();
virtualvoid OnEvent(SDL_Event* Event);
virtual ~PushButton();
};
#endif
PushButton is derived from UIGameObject which is derived from BaseGameObject but according to Visual Studio PushButton doesn't have access to any of the methods contained inside BaseGameObject.
Could anyone help me fix this problem? And maybe give me some tips on how to improve my inheritance setup?
1>g:\visual studio 2008\projects\sdl engine\sdl engine v2.0\entities.cpp(79) : error C2509: 'LoadImage' : member function not declared in 'PushButton'
1> g:\visual studio 2008\projects\sdl engine\sdl engine v2.0\entities.h(45) : see declaration of 'PushButton'
I even tried changing it to
class PushButton : public UIGameObject, public BaseGameObject
But then it told me that I had ambiguous variables and function re-definitions.
Show entities.cpp, line 79 and a couple of surrounding lines, please ;-). Maybe you have a problem in the calling code?
Ciao, Imi.
PS: BaseGameObject(std::string Filename
The paramter should be a "normal" std::string if (and only if) you are going to change the variable within the function. If you are not changing "Filename", use "const std::string&" for better performance. Make this a habbit for all classes you pass as parameter ;)
Ah ya, I just took a look at my entities.cpp, I had tried to define a LoadImage() function for the PushButton class.
About using references though... I had thought that was somewhat unsafe since once you handed it the reference you had no way to control if the memory gets released on you(Like a string created in the local scope of a function).