If I wish to clone a polymorphic object in C++ (i.e. a clear case of a category A which comes from another class B), the simplest way appears to provide B an online clone member function, that needs to be overridden with a and appears such as this

A* clone(){
    return new A(*this);

My issue is, which i find this unnecessary boilerplate code, because this is more often than not needed, if a person really wants to use run-time polymorphic options that come with C++. How will it be circumvented?


Why I want this:

My use situation could be abstracted towards the following example: I've got a class Integral, which examines the integral of some function. Do do that, there is a member the industry pointer towards the class MathFunction. This abstract class consists of a pure virtual function evaluate that takes one argument. I I needed to implement the energy function I'd produce a class PowFunction : class MathFunction. These kinds might have an associate exponent and also the function evaluate would seem like this:

double evaluate(x){
    return pow(x,exponent);

As mentioned the member MathFunction of class Integral needs to be polymorhpic, which requires that it is a pointer. To reply to the questions from the commenters with another question. Why wouldn't I wish to have the ability to make copies of MathFunction objects?

I truly want the Integral to "own" its MathFunction, meaning, that it may affect the parameters (like exponent) without altering the MathFunction associated with a other Integral object. What this means is every Integral will need its very own copy. This involves a clone() function for MathFunctions, does not it?

One alternative i figured of: If several Integral objects can share exactly the same MathFunction using a pointer towards the same address, I possibly could create copies of Integral objects with no need to copy the MathFunction. However in this situation I would need to make all of the qualities const or in some way readonly, that is not so elegant either. Also, which Integral object should handle remove the MathFunction object?

The reason why you need this:

Are you currently seriously saying, that the moment you train with polymorphic objects you do not ever require a copy operation? Why is polymorphic object not the same as other objects in this way?

By using this argumentation, you might toss the copy constructor and copy assignment operator from the C++ standard!

Reduce the necessity to clone polymorphic objects. Really, I rarely find the requirement for this during my own code, and also the comments around the question claim that I am hardly alone within the opinion that when you are cloning everything, you are most likely creating it wrong.

Obviously, not to be unhelpful, you can indeed make use of the Strangely enough Recurring Template Pattern.

template<typename T> struct Clone {
    virtual T* clone() { return new T(static_cast<const T&>(*this)); }

I handled this problem having a macro... it's ugly, however it activly works to avoid incongruencies.

/** An interface that can be inherited by any class that wants to provide a Clone()
  * method that will return a copy of itself.
class ICloneable
   ICloneable() {}
   virtual ~ICloneable() {}

   virtual ICloneable * Clone() const = 0;
#define DECLARE_STANDARD_CLONE_METHOD(class_name) virtual ICloneable * Clone() const {new class_name(*this);}

public MyCloneableClass : public ICloneable
   MyCloneableClass() {}