Copying derived entities using only base class pointers, (without exhaustive testing!) - C++

14,606

Solution 1

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.

Solution 2

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();
}
Share:
14,606
geoff3jones
Author by

geoff3jones

Updated on June 07, 2022

Comments

  • geoff3jones
    geoff3jones about 2 years

    Given a base class that is inherited by plethora of derived classes, and a program structure that requires you manage these via base class pointers to each entity. Is there a simple way to copy the entire derived object when only the base class pointer is known?

    Looking around it would seem possible (if incredibly tedious) to use the dynamic_cast call to check if a base pointer can be cast as the appropriate derived class, then copy it using the derived class's copy constructor. However this is not really an optimal solution partly due to the excessive use of dynamic_cast and also it would see a total headache to maintain and extend.

    Another more elegant solution I have come across is as follows:

    class Base
    {
    public:
       Base(const Base& other);
       virtual Base* getCopy();
       ...
    }
    
    class Derived :public Base
    {
       Derived(const Derived& other);
       virtual Base* getCopy();
       ...
    }
    
    Base* Base::getCopy()
    {
       return new Base(*this));
    }
    
    Base* Derived::getCopy()
    {
       return static_cast<Base*>(new Derived(*this));
    }
    

    Then by calling getCopy() on the Base class pointer to any derived object one still gets a base class pointer back but also the whole of the derived object has been copied. This method feels a lot more maintainable as it just requires a similar getCopy() function to be in all derived classes, and does away with the need to test against all possible derived objects.

    Essentially, is this wise? or is there a better, even neater way of doing this?

  • templatetypedef
    templatetypedef over 13 years
    I wouldn't say that dynamic_cast is always preferred. For upcasting it's not preferable to static_cast when non-virtual inheritance is being used, and when the cast is known to be safe it's an unnecessary runtime check. I'd say that dynamic_cast is best for downcasts where it's unclear whether the cast is going to succeed. In this case, it's a safe upcast, which is actually probably best done without the explicit casting at all.
  • UncleBens
    UncleBens over 13 years
    What if I want to inherit further classes from D1 or D2?
  • geoff3jones
    geoff3jones over 13 years
    Thank you for the excelent reply, and for the tips on my actually code. I was not aware of covariant types but I will definately read up on them now.
  • Daniel Gonzalez
    Daniel Gonzalez over 13 years
    Then it probably doesn't help you as much. You can still override clone() manually on a derived class. And you can skip or use Cloneable<T> as you see fit.
  • geoff3jones
    geoff3jones about 7 years
    The downside of this in the context of my original question is it requires the concrete derived type to be visible at the location you wish to copy it. In this question I was looking for a way to copy derived objects when all you can see are base class pointers/references. Some other points with your implementation: the dynamic cast is superfluous you can as explained by @templatetypedef just do Base* ptr = new T(other);. The other possible downside here the use of a naked new allocating the copy on the heap in an unsafe way (shared_ptr/unique_ptr would at least offer some protection).
  • tharinduwijewardane
    tharinduwijewardane about 6 years
    For upcasting you don't need static_cast or dynamic_cast. Its implicitly done
  • geoff3jones
    geoff3jones over 3 years
    Is this not the same answer as provided by @Martin-Stone just using shared and unique pointers instead of raw?
  • dragonxlwang
    dragonxlwang over 3 years
    yes, the idea is the same. I overlooked @Martin-Stone's answer
  • dragonxlwang
    dragonxlwang over 3 years
    @UncleBens, you will need to virtual inherent from the Cloneable stackoverflow.com/questions/9422760/…
  • dragonxlwang
    dragonxlwang over 3 years
    would better explicit note in the answer of using CRTP