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); 
    }  

    Circle* createSimilar() const 
    
        return new Circle(); 
    }  
    
    //...  
}; 

The invocation of "Circle(*this)" is that of copy construction ("*this" has type "const Circle&" in these methods). "createSimilar()" is similar, but it constructs a "default" Circle.

Users use these as if they were "virtual constructors":  

void userCode(Shape& s)  
{  
    Shape* s2 = s.createCopy();  
    Shape* s3 = s.createSimilar();  
    //...  delete s2; //relies on destructor being virtual!!  
    delete s3; // ditto  
}  

This fn will work correctly regardless of whether the Shape is a Circle, Square, or some other kind-of Shape that doesn't even exist yet.

Comments

Popular posts from this blog

GCC Extenstion

What is Code Bloat?