How to create a new pointer to a derived class which type is unknown at compile time in C++? : <p>I have a code where there are some pointers to derived classes of the same base class. At one point I need to create another pointer to one of this derived classes, but I don't know at compile time to which class it will point to.
Here an example of the code:</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;

class Polygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b; };
    virtual int area() = 0;
};

class Rectangle: public Polygon {
  public:
    int area()
      { return width*height; }
};

class Triangle: public Polygon {
  public:
    int area()
      { return width*height/2; }
};

int main () {
  Rectangle* rect = new Rectangle;
  Triangle* trgl = new Triangle;
  Polygon* ppoly;
  trgl-&gt;set_values (4,5);
  ppoly = trgl;
  cout &lt;&lt; ppoly-&gt;area() &lt;&lt; '\n';
  trgl-&gt;set_values (8,5);
  cout &lt;&lt; ppoly-&gt;area() &lt;&lt; '\n';
  return 0;
}
</code></pre>

<p>Writing the code as above, 'ppoly' points to the same memory as trgl, so the 2 lines <code>cout &lt;&lt; ppoly-&gt;area() &lt;&lt; '\n'</code> print different results because I have changed width of the pointer <code>trgl</code>. In my codeI don't want this behaviour, but I would like that <code>ppoly</code> is a new pointer of the same type of <code>trgl</code>.
I have tried to use <code>Polygon* ppoly = new Polygon</code>, but returns me an error at compile time: </p>

<blockquote>
  <p>error: invalid new-expression of abstract class type 'Polygon'</p>
</blockquote>

<p>How can I correct the software?</p>
