I have code like following
class GameObject
{
public:
virtual void update(...) = 0;
virtual void render(...) = 0;
};
class Sprite : public GameObject
{
public:
Sprite();
~Sprite()
{
}
void setSrcRect()noexcept
{
//Some code
}
void setDstRect()noexcept
{
//Some code
}
uint32_t getWidth() const noexcept { return width_; }
uint32_t getHeight() const noexcept { return height_; }
virtual void update(...) override
{
//Some code
}
virtual void render(...) override
{
//Some code
}
};
Later on I create Object like
GameObject* obj = new Sprite();
problem is that I am not able to call methods like
obj->getWidth();
I can add those methods in GameObject but I want to keep it pure interface. Only having pure virtual methods. I am keeping GameObject like this because it will give me advantage when later on in game I have more objects.
I can just store them
std::vector<GameObject*> actors;
in render I can do
for(const auto actor: actors)
{
actor->render();
}
Also as currently sprite is there could be one more GameObject SpriteSheet and so on. What I can do to achieve this? Keep GameObject as it is but add methods to classes inherited from it.
I do understand that type of sprite is still GameObject and method calls can only be resolved at runtime. So I am requesting for any alternative methods to achieve this?