Hello,
I am having trouble with using polymorphism:
#include <list>
#include <iostream>
class a {
virtual void foo() { std::cout << "a" };
}
class b {
void foo() { std::cout << "b" };
std::list<a> listOfA;
b objectOfB;
listOfA.push_front(objectOfB);
for (std::list<a>::iterator it = listOfA.begin(); it != listOfA.end(); ++it)
it->foo();
I would like this code to produce "b" as output, but in my code i always get "a". Could anyone point me to the problem/solution/alternative? I cant seem to find one using search. BTW this is in a context of storing objects derived from a base class i.e. bullets, players, enemies, bricks etc. in a tank game.
Your help is greatly appreciated.