I want to make a copy of an object that is passed to me
via a base class pointer. No slicing allowed, so the copy
has to be the same dynamic type as the original. How do I
do this?
base_class * clone(const base_class * orig)
{
base_class * p;
... // A miracle occurs (HOW?)
// And now p points to a copy of *orig,
// with the same dynamic type as *orig.
return p;
}
Now I want to be able to do this:
class base_class { ... };
class derived_class : public base_class { ... };
derived_class x;
base_class * newxp = clone(&x);
// Now newxp points to a copy of x.
// The copy has type derived_class.