Store derived class objects in base class variables
问题 I would like to store instances of several classes in a vector. Since all classes inherit from the same base class this should be possible. Imagine this program: #include <iostream> #include <vector> using namespace std; class Base { public: virtual void identify () { cout << \"BASE\" << endl; } }; class Derived: public Base { public: virtual void identify () { cout << \"DERIVED\" << endl; } }; int main () { Derived derived; vector<Base> vect; vect.push_back(derived); vect[0].identify();