I have a base class and several inherited classes.
class Base {
public:
void update();
};
class Inherited1 {
public:
void update(); // override base method
};
class Inherited2 {
public:
void update(); // override base method
};
I have a vector of the base class containing pointers to several instances of the subclasses, i.e.
vector<Base> myObjects;
Base *i1 = new Inherited1();
Base *i2 = new Inherited2();
myObjects.push_back(*i1);
myObjects.push_back(*i2);
I want to be able to go through each element in the vector and automatically call the subclass (i.e. overriden) methods, like:
for (int i=0; i<myObjects.size(); i++) {
(*myObjects[i]).update();
}
But this doesn't work -- it just calls the superclass method. What is the proper way to achieve this?