I'm relatively new to C++, and coming from a C# background I'm having trouble with this list iteration:
I have one method which loops through a list of objects and calls an update method for each one, which works great. The list is of type std::list<EngineComponent>
and is called engineComponents
.
void Game::Update()
{
for (EngineComponent component: this->engineComponents)
{
component.Update();
}
}
I also have a subclass of EngineComponent
called DrawableEngineComponent
.
The issue appears when I try to do a similar iteration:
void Game::Draw()
{
for (DrawableEngineComponent component: this->engineComponents)
{
component.Draw();
}
}
This produces the error "No suitable user-defined conversion from 'EngineComponent' to 'DrawableEngineComponent' exists". Given that this implementation was all fine and dandy in C#, I'm not really sure how best to address this issue in C++.
I can think of a few alternative ways that would/should work, but I'm wondering if there's functionality in C++ to do this in a way similar to C# without having to manually define a conversion.
The definitions for the two classes concerned are as follows:
class EngineComponent
{
public:
EngineComponent(void);
~EngineComponent(void);
virtual void Update(void);
};
class DrawableEngineComponent : public EngineComponent
{
public:
DrawableEngineComponent(void);
~DrawableEngineComponent(void);
virtual void Draw(void);
};
And yes, I am copying the XNA framework a bit ;)