Copying derived entities using only base class pointers, (without exhaustive testing!) - C++
This approach is the preferred way of copying polymorphic objects because it offloads the responsibility of determining how to copy an object of an arbitrary type to that object, rather than trying to determine it at compile-time. More generally, if you don't know what the base class pointer points at at compile-time, you can't possibly know which of the many potential pieces of code you would need to execute in order to get a correct copy. Because of this, any working solution will need a dynamic selection of code, and the virtual function is a good way to do this.
Two comments on your actual code. First, C++ inheritance allows a derived class overriding a base class member function to have the derived function return a pointer of a type more specific than the base class version. This is called covariance. As an example, if a base class function is
virtual Base* clone() const;
Then a derived class can override it as
virtual Derived* clone() const;
And this will work perfectly fine. This allows you, for example, to have code like this:
Derived* d = // something...
Derived* copy = d->clone();
Which, without the covariant overload, wouldn't be legal.
Another detail - in the code you have, you explicitly static_cast
the derived pointers to base pointers in your code. This is perfectly legal, but it's not necessary. C++ will implicitly convert derived class pointers to base class pointers without a cast. If, however, you use the covariant return type idea, this won't come up because the return type will match the type of the objects you'll be creating.
Note that you don't need the static_cast there. Derived* converts to Base* implicitly. You absolutely shouldn't use a dynamic_cast for that, as Ken Wayne suggests, since the concrete type is known at compile time, and the compiler can tell you if the cast is not allowed.
As for the approach, this pattern is standard enough to be built in to C# and Java as ICloneable and Object.clone(), respectively.
Edit:
... or is there a better, even neater way of doing this?
You could use a "self-parameterized base class", which saves you implementing the clone() function each time. You just need to implement the copy constructor:
#include <iostream>
struct CloneableBase {
virtual CloneableBase* clone() const = 0;
};
template<class Derived>
struct Cloneable : CloneableBase {
virtual CloneableBase* clone() const {
return new Derived(static_cast<const Derived&>(*this));
}
};
struct D1 : Cloneable<D1> {
D1() {}
D1(const D1& other) {
std::cout << "Copy constructing D1\n";
}
};
struct D2 : Cloneable<D2> {
D2() {}
D2(const D2& other) {
std::cout << "Copy constructing D2\n";
}
};
int main() {
CloneableBase* a = new D1();
CloneableBase* b = a->clone();
CloneableBase* c = new D2();
CloneableBase* d = c->clone();
}