Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6002827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:02:03+00:00 2026-05-23T01:02:03+00:00

If I want to clone a polymorphic object in C++ (i.e. an instance of

  • 0

If I want to clone a polymorphic object in C++ (i.e. an instance of a class A which is derived from some other class B), the easiest way seems to give B a virtual clone member function, that has to be overridden by A and looks like this

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

My problem is, that I find this unnecessary boilerplate code, as this is almost always needed, if one wants to use run-time polymorphic features of C++. How can it be circumvented?

Thanks

Why I need this:

My use case can be abstracted to the following example:
I have a class Integral, which evaluates the integral of some function. Do do this, they have a member which is a pointer to the class MathFunction. This abstract class contains a pure virtual function evaluate which takes one argument. I I wanted to implement the power function I would create a class PowFunction : class MathFunction. This class would have a member exponent and the function evaluate would look like this:

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

As stated the member MathFunction of class Integral has to be polymorhpic, which requires it to be a pointer. To answer the questions of the commenters with another question. Why wouldn’t I want to be able to make copies of MathFunction objects?

I really want the Integral to “own” its MathFunction, meaning, that it can alter the parameters (like exponent) without changing the MathFunction of any other Integral object. This means every Integral needs to have its own copy. This requires a clone() function for MathFunctions, doesn’t it?

One alternative i thought of: If several Integral objects can share the same MathFunction via a pointer to the same address, I could create copies of Integral objects without the need to copy the MathFunction. But in this case I would have to make all the properties const or somehow readonly, which is not very elegant either. Also, which Integral object should handle delete the MathFunction object?

Why you need this:

Are you seriously saying, that as soon as you work with polymorphic objects you don’t ever need a copy operation? What makes polymorphic object different from other objects in this respect?

Using this argumentation, you could also throw the copy constructor and copy assignment operator out of the C++ standard!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:02:04+00:00Added an answer on May 23, 2026 at 1:02 am

    I handled this issue with a macro… it’s ugly, but it works to avoid inconsistencies.

    /** 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
    {
    public:
       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
    {
    public:
       MyCloneableClass() {}
    
       DECLARE_STANDARD_CLONE_METHOD(MyCloneableClass);
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a window (derived from JFrame) and I want to disable the close
I've got a git-svn clone of an svn repo, and I want to encourage
I want to move the form title, icon and close, and help buttons from
public class Example { public Example duplicate; public void duplicateState() { this.example = this.clone();
I want to close a System.Windows.Forms.Form if the user clicks anywhere outside it. I've
I want to close a tab in my tab control when the mouse wheel
I don't want a close window menu item in the task bar context menu
I want to be able to close an alert box automatically using Javascript after
I want to show a chromeless modal window with a close button in the
Want to know what the stackoverflow community feels about the various free and non-free

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.