I am working on a graphical application that makes extensive use of virtual classes. He has:
An image class, which is essentially a collection of shapes.
A shape class that is purely virtual and has several classes that inherit it:
The shape of a shape, which is any graphic shape (also virtual), inherits this shape.
Essentially, my problem boils down to implementing an image class, which is mainly used to store a collection of shapes. I am currently using Vector to store shapes, but obviously this is not the right solution, since Vector creates these shapes, which is not very good, since they are purely virtual.
Below is my current code base (short description):
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<Point> points; }; class Picture: public Figure { public: ... Picture(Graphics& gd); Picture (const Picture&); ~Picture(); void clear(); void add (const Shape&); ... private: std::vector<Shape> shapes; Graphics* gfx; };
The error messages I receive are just one of them:
picture.cpp: 33: instance 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 select an object abstract type 'Shape' shape.h: 12: note: because the following virtual functions are clean in shape: shape.h: 58: note: virtual void Shape :: get (std :: istream &) shape.h: 31: note: virtual void Shape :: put (std :: ostream &) const shape.h: 36: note: virtual void Shape :: scale (const Point &, 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 Form :: 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 &) const
So what is the ideal way to store these shapes. What collection should I use to store these things?
thanks
c ++ vector virtual-inheritance virtual
the_gastropod
source share