I am learning C++ in class and am working on trying to return a pointer to a derived object using a virtual create/clone function.
I found this implementation and am about to build around it http://www.cs.uregina.ca/Links/class-info/210/C++FAQ/virtual-functions.html#[20.5]
What I don't understand is how does the pointer returned actually point to any relevant data after clone() or create() exit their block?
Is there a better approach to this or is this where I need to program a function per case?
Thanks much
class Shape {
public:
virtual ~Shape() { } // A virtual destructor
virtual void draw() = 0; // A pure virtual function
virtual void move() = 0;
// ...
virtual Shape* clone() const = 0; // Uses the copy constructor
virtual Shape* create() const = 0; // Uses the default constructor
};
class Circle : public Shape {
public:
Circle* clone() const { return new Circle(*this); }
Circle* create() const { return new Circle(); }
// ...
};