Posts

Showing posts with the label Virtual Constructors

Virtual Constructors.

What is a "Virtual Constructor"?   An idiom that allows you to do something that C++ doesn't directly support. You can get the effect of virtual constructor by a virtual  "createCopy()"  member fn (for copy constructing), or a virtual  "createSimilar()"  member fn (for the default constructor).   class Shape  {        public:  virtual ~Shape() { } //see on "virtual destructors" for more        virtual void draw() = 0;        virtual void move() = 0;  //...        virtual  Shape* createCopy() const = 0;        virtual Shape* createSimilar() const = 0;   };   class Circle : public Shape  {        public:  Circle* createCopy() const          {            return new Circle(*this);   ...