I tried to add objects to the "content" vector, and use show() on all of them. But the objects that are children (A, B) of "Base" behave like they were of "Base" type, what is not my intention. As it seems, I tried to use virtual functions but it doesn't work.
I hope that the code will speak for itself.
class Base {
public:
virtual void show() { cout << "Base "; }
};
class A : public Base {
public:
virtual void show() { cout << "A "; }
};
class B : public Base {
public:
virtual void show() { cout << "B"; }
};
vector<Base> content;
void add(Base &o) {
content.push_back(o);
}
A test1;
B test2;
add(test1);
add(test2);
for (size_t i = 0; i < content.size(); i++) {
collection[i].show(); // output is: Base Base instead of A B
}
Thanks in advance.