title: How to store objects of multiple subclasses in a vector in C++
body: <p>I'm trying to create any iterable to store subclass objects in. </p>
  
  <p>I currently am using a vector of pointers to the superclass, but this doesn't seem to allow any of the subclass-specific declarations to be used. </p>
  
  <pre><code>class piece
  {
  public:
      int foo()
      {
          return 1;
      }
  };
  
  class pawn : public piece 
  {
  public:
      int foo()
      {
          return 2;
      }
  };
  
  std::vector&lt;piece*&gt; pieces;
  pieces.push_back(new pawn());
  
  pieces[0]-&gt;foo(); // returns 1 though pieces[0] should be a pawn
  </code></pre>
  
  <p>The call to pieces[0]->foo() returns 1, when I want it to return 2.</p>
  
  <p>Debugging shows that the "new pawn()" creates a piece pointer, so I get why it doesn't use the pawn function, but I don't know why it isn't a pawn in the first place.</p>