title: Pure Virtual Class and Collections (vector?)
body: <p>I'm working on a graphics application that is using virtual classes fairly extensively. It has:</p>
  
  <ul>
  <li><p>A picture class, which is essentially a collection of shapes.</p></li>
  <li><p>A shapes class, which is purely virtual and has a few classes that inherit from it:</p>
  
  <ul>
  <li>Circle</li>
  <li>Polygon</li>
  <li>Rectangle</li>
  </ul></li>
  <li><p>A Figure shape, which is any graphical figure (also virtual), shape inherits from this.</p></li>
  </ul>
  
  <p>Essentially, my problem comes down to implementing the picture class, which is basically being used to store a collection of shapes. I'm currently using a Vector to store shapes, however, it's apparent that this is the wrong decision since Vector instantiates these shapes, which is not good as they are purely virtual.</p>
  
  <p>Below is my current code base (summarized a bit):</p>
  
  <pre><code>class Figure {
  public:
  ...
    virtual ~Figure();
  ...
  };
  
  class Shape: public Figure
  {
  public:
  ...
    virtual ~Shape() {}
    virtual Shape* clone() const = 0;
  ...
  };
  
  class Polygon : public Shape
  {
  public:
  ...
  virtual Shape* clone() const {return new Polygon(*this);}
  ... 
  private:
  std::vector&lt;Point&gt; points;
  
  };
  
  class Picture: public Figure {
  public:
  ...
    Picture(Graphics&amp; gd);
    Picture (const Picture&amp;);
    ~Picture();
    void clear();
    void add (const Shape&amp;);
  ...
  private:
  std::vector&lt;Shape&gt; shapes;
  Graphics* gfx;
  };
  
  //Picture implementation:
  ...
  Picture::Picture(Graphics&amp; gd)
  {
  gfx = &amp;gd;
  }
  
  
  Picture::Picture(const Picture&amp; a)
  {
  shapes = a.shapes;
  }
  
  Picture::~Picture() 
  {
  clear();
  }
  
  void Picture::clear()
  {
  shapes.clear();
  }
  
  void Picture::add (const Shape&amp; shp)
  {
  Shape* nshp = shp.clone();
  shapes.push_back(*nshp);
  }
  ...
  </code></pre>
  
  <p>The error messages I'm getting are just a bunch of these:</p>
  
  <blockquote>
    <p>picture.cpp:33:   instantiated from
    here
    /opt/local/bin/../lib/gcc/sparc-sun-solaris2.10/4.4.1/../../../../include/c++/4.4.1/ext/new_allocator.h:105:
    error: cannot allocate an object of
    abstract type 'Shape' shape.h:12:
    note:   because the following virtual
    functions are pure within 'Shape':
    shape.h:58: note:       virtual void
    Shape::get(std::istream&amp;) shape.h:31:
    note:       virtual void
    Shape::put(std::ostream&amp;) const
    shape.h:36: note:       virtual void
    Shape::scale(const Point&amp;, double)
    shape.h:40: note:       virtual void
    Shape::translate(double, double)
    shape.h:45: note:       virtual void
    Shape::reflectHorizontally(double)
    shape.h:49: note:       virtual void
    Shape::reflectVertically(double)
    shape.h:52: note:       virtual
    RectangularArea Shape::boundingBox()
    const shape.h:21: note:       virtual
    Shape* Shape::clone() const
    shape.h:55: note:       virtual void
    Shape::draw(Graphics&amp;) const</p>
  </blockquote>
  
  <p>So what is the ideal way to store these shapes. What kind of collection should I be using to store these things? </p>
  
  <p>Thanks</p>