I tried the thing with iterator... this code works fine for me, so if your problem persists, consider different compiler =)
#include <iostream>
#include <vector>
class Base
{
public:
virtual void f()
{
std::cout << "Base\n";
}
};
class Derived: public Base
{
public:
virtual void f()
{
std::cout << "Derived\n";
}
};
int main()
{
std::vector<Base*> s;
s.push_back(new Base());
s.push_back(new Derived());
s.push_back(new Base());
s.push_back(new Derived());
s.push_back(new Base());
s.push_back(new Derived());
for (std::vector<Base*>::iterator it = s.begin(); it != s.end(); ++it)
{
(*it)->f();
}
return 0;
}
the output I have:
Base
Derived
Base
Derived
Base
Derived
also you might try to cast the pointers by dynamic_cast<>
, even though it defeats the whole point of using virtual methods. Good luck.