Is it possible to clone a polymorphic object without manually adding overridden clone method into each derived class in C++?

Calmarius :

The typical pattern when you want to copy a polymorphic class is adding a virtual clone method and implement it in each derived class like this:

Base* Derived::clone()
{
    return new Derived(*this);
}

Then in a calling code you can:

Base *x = new Derived();
Base *y = x->clone();

However if you have 50+ derived classes and realize you need polymorphic copy, it's tedious to copy-paste the cloning method into each of them. And it's essentially a boilerplate that works around a language limitation that you have to spell out the actual name to call the constructor.

I haven't keep track with the new features in recent C++ standards... Is there a way to avoid this in modern C++?

n. 'pronouns' m. :

You can use this generic CRTP code

template <class Derived, class Base>
struct Clonable : Base {
    virtual Base* do_clone() {
        return new Derived(*static_cast<Derived*>(this));
    }
    Derived* clone() { // not virtual
        return static_cast<Derived*>(do_clone());
    }

    using Base::Base;
};

struct empty {};
struct A : Clonable<A, empty> {};
struct B : Clonable<B, A> {};

It can be generalised to smart pointers and multiple bases if desired.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cloning of an object using clone() method of Object class

Why not cast inside overridden .clone?

The method clone() from object is not visible?

Does the clone method clone overridden methods?

State of Derived class object when Base class constructor calls overridden method in Java

How does Object class implement clone() method

Prevent writing clone method for each subclass

Clone or copy a method in an object (Javascript)

Clone a object while maintaining derived type from an extension method

Why does the derived clone() method return a reference?

accessing overridden method from derived class object

Clone derived class from base class pointer

Clone derived class from base class method

Is it possible to clone a single tone class's object?

Calling an overridden method in a derived class

How to clone a object to a object of Object class?

Is it possible to clone jquery DOM object with props?

Java - How to implement method clone for base and derived classes

No clone method in String Class

Is a Git clone without content possible?

Clone an instance of a class, then clone the clone, then clone the clone...?

Trying to clone an HTML class using jquery but each method I use the clone copies an infinite number of times

How clone() method of object class works?

How to call the overridden method of a derived class (child class)?

Executing base class overridden method in derived class

c++ polymorphic method defined in derived class matches type that should match to the parent

Clone new object of old object without old reference in C#

Overridden method in derived class

Unable to clone an object from within an abstract class method in Scala