Here's a sample program for what I'm talking about:
#include <iostream>
#include <list>
using namespace std;
class Thing {
public:
virtual void jump() { cout << "Called from Thing class" << endl; }
};
class Car: public Thing {
public:
virtual void jump() { cout << "Vroom vroom; imma car biotch" << endl; }
};
int main(int argc, char* argv[]) {
Car myCar;
list<Thing> myList;
myList.push_back(myCar);
std::list<Thing>::iterator iterator;
for (iterator = myList.begin(); iterator != myList.end(); ++iterator) {
iterator->jump();
}
return 0;
}
Output:
Called from Thing class
What I want to do is make a list of "Things". I want to be able to add instances of the child class from the Thing class; then I want to be able to call their overwritten function from an iterator.
The only issue is, even while using the "virtual" keyword, the iterator uses the Thing::jump function as opposed to the Car::jump function.
What can I do to make it not only work with Car, but all potential child classes from Thing, Car, etc. that overwrite the "jump" function?