title: Vector of pointers to objects c++
body: <p>I have a base class with 2 derived classes and another one which store objects of the derived classes type:</p>
  
  <pre><code>class X
  {};
  
  class X1 : public X
  {};
  
  class X2 : public X
  {};
  
  class Y
  {
  std::vector&lt;X*&gt; V;
  };
  </code></pre>
  
  <p>why doesn't this method of introducing elements in V work?</p>
  
  <pre><code>X1 object;
  X *ptr = &amp;object;
  V.push_back(ptr);
  </code></pre>