I'm trying a bit with polymorphism with the following code snippet:
#include <iostream>
#include <vector>
using namespace std;
struct Foo {
virtual void f() {
cout << "f from Foo" << endl;
}
};
struct Bar : public Foo {
void f() {
cout << "f from Bar" << endl;
}
};
int main() {
Foo foo;
Bar bar;
vector<Foo> fooV;
fooV.push_back(foo);
fooV.push_back(bar);
for(auto it = fooV.begin(); it != fooV.end(); ++it)
it->f();
}
I expected that since f()
is overridden, one of them would print "f from Foo" and the other "f from Bar". However, the output of the program is
f from Foo
f from Foo
I should also note that I also tried adding the keyword override
to the declaration of f()
in Bar
, since I'm using C++11. It didn't have any effect.