I have the next code
class Shape
{
public:
virtual Shape* create() { return new Shape; }
virtual void print() { cout << "Shape" << endl; }
virtual ~Shape() {}
};
class Circle : public Shape
{
public:
virtual Circle* create() { return new Circle; }
virtual void print() { cout << "Circle" << endl; }
};
void foo ()
{
Shape* sp = new Circle;
Circle* cp = sp->create();
cp->print();
delete sp;
delete cp;
}
The code is not compiling because Shape is not a Circle(downcasting error).
I am a little confused. The create() is not dynamic binding? the line
Circle* cp = sp->create();
not suppose to return a Circule*?