The solution to this problem is the virtual copy constructor; which is not allowed in C++ (It is not allowed because constructor is something related to the class itself, it can't be delegated to class's children). That why clone
is the best fit for this problem.
Here is a similar answer, with most popular syntax.
So, you code shall be
class Meal {
public:
virtual void cook() = 0; // pure virtual
virtual Meal* clone() const = 0; // pure virtual clone.
};
class Omelette : public Meal {
public:
void cook() {
/*do something*/
}; // non-virtual
virtual Omelette* clone() const
{
return new Omelette(*this);
}
};
class Waffle : public Meal {
public:
void cook() {
/*do something*/
}; // non-virtual
virtual Waffle* clone() const
{
return new Waffle(*this);
}
};
std::vector< std::unique_ptr<Meal> > menu;
void addMeal(const Meal& meal) {
menu.emplace_back( meal.clone() ); // cannot allocate an object of abstract type
}