title: Store derived class objects in base class variables
body: <p>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.</p>
  <p>Imagine this program:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  class Base
  {
      public:
      virtual void identify ()
      {
          cout &lt;&lt; &quot;BASE&quot; &lt;&lt; endl;
      }
  };
  
  class Derived: public Base
  {
      public:
      virtual void identify ()
      {
          cout &lt;&lt; &quot;DERIVED&quot; &lt;&lt; endl;
      }
  };
  
  int main ()
  {
      Derived derived;
      
      vector&lt;Base&gt; vect;
      vect.push_back(derived);
      
      vect[0].identify();
      return 0;
  }
  </code></pre>
  <p>I expected it to print <code>&quot;DERIVED&quot;</code>, because the <code>identify()</code> method is <code>virtual</code>. Instead <code>vect[0]</code> seems to be a <code>Base</code> instance and it prints <code>&quot;BASE&quot;</code>.</p>
  <p>I guess I could write my own container (probably derived from <code>vector</code>) somehow that is capable of doing this (maybe holding only pointers...).</p>
  <p>I just wanted to ask if there is a more C++'ish way to do this. AND I would like to be completely <code>vector</code>-compatible (just for convenience if other users should ever use my code).</p>