In other words, given a base class shape and a derived class rectangle:
class shape
{
public:
enum shapeType {LINE, RECTANGLE};
shape(shapeType type);
shape(const shape &shp);
}
class rectangle : public shape
{
public:
rectangle();
rectangle(const rectangle &rec);
}
I’d like to know if I could create an instance of rectangle by calling:
shape *pRectangle = new shape(RECTANGLE);
and how could I implement the copy constructor, in order to get a new rectangle by calling:
shape *pNewRectangle = new shape(pRectangle);
Short Answer: No
Long Answer:
You need a factory object/method.
You can add a static factory method to the base class the creates the appropriate object type.
Personal preference:
I would go with a completely different class to be the factory rather than using a static method on the base class. The reason for this is that every time you create a new Shape class the above style forces you to re-build the Shape class each time.
So I would separator out the factory into a ShapeFactory class.