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

  • SEARCH
  • Home
  • 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 892155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:00:26+00:00 2026-05-15T14:00:26+00:00

In C++, I find it very useful to add a Clone() method to classes

  • 0

In C++, I find it very useful to add a “Clone()” method to classes that are part of hierarchies requiring (especially polymorphic) duplication with a signature like this:

class Foo {
public:
virtual Foo* Clone() const;
};

This may look pretty, but it is very prone to bugs introduced by class maintenance. Whenever anyone adds a data member to this class, they’ve got to remember to update the Clone() method. Failing to results in often subtle errors. Unit tests won’t find the error unless they are updated or the class has an equality comparison method and that is both unit tested and was updated to compare the new member. Maintainers won’t get a compiler error. The Clone() method looks generic and is easy to overlook.

Does anyone else have a problem with Clone() for this reason? I tend to put a comment in the class’s header file reminding maintainers to update Clone if they add data members. Is there a better idea out there? Would it be silly/pedantic to insert a #if/etc. directives to look for a change to the class size and report a warning/error?

  • 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-15T14:00:27+00:00Added an answer on May 15, 2026 at 2:00 pm

    Why not just use the copy constructor?

    virtual Foo* Clone() const { return new Foo(*this); }
    

    Edit:
    Oh wait, is Foo the BASE class, rather than the DERIVED class? Same principle.

    virtual Foo* Clone() const { return new MyFavouriteDerived(*this); }
    

    Just ensure that all derived classes implement clone in this way, and you won’t have a problem. Private copy constructor is no problem, because the method is a member and thus can access it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is my first time here (I find the website very useful). I'm new
There are some very useful classes for working with images. I am stuck with
SQL Server XML Schema Collection is an interesting concept and I find it very
I just switched to CMake. And yet found it very useful and realized some
i want to add a data indicator line similar to the following image in
I work in a team where we use extensive ruleset in StyleCop and I
I am attempting to complete the following how-to in order to try and understand
Original Problem In building our projects, I want the mercurial id of each repository
I'm fairly new to actual programming languages, and Python is my first one. I

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.