Hi all,
I was reading Game engine architecture and with that, brushing up some coding theory.
If I recall correct, polymorphism can help you for example to achieve the following:
- create a parent class with a virtual function, that needs to implemented in child/ inherited classes
- let's take a shape for example
- I have a triangle and square class implementing their own Draw function, like declared a virtual function in the shape class
Now my question is, how do I iterate through all child class objects?
Simplified code:
class Shape
{
public:
unsigned int someVar;
virtual void Draw();
};
class Triangle : public Shape
{
public:
void Draw();
};
class Square : public Shape
{
public:
void Draw();
};
// some program
std::vector<Triangle> myTriangles;
std::vector<Square> mySquares;
// generate some triangles and squares
for(size_t i=0;i<myTriangles.size();++i) myTriangles[i].Draw();
for(size_t j=0;j<mySquares.size();++j) mySquares[j].Draw();
I believe it should be possible to do the last 2 lines at once.
How would I iterate through them at once, using the advantage that polymorphism should bring here?