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 210477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:00:32+00:00 2026-05-11T18:00:32+00:00

Here’s a Clone() implementation for my class: MyClass^ Clone(){ return gcnew MyClass(this->member1, this->member2); }

  • 0

Here’s a Clone() implementation for my class:

    MyClass^ Clone(){
        return gcnew MyClass(this->member1, this->member2);
    }

Now I have about 10 classes derived from MyClass. The implementation is the same in each case. Owing to the fact that I need to call gcnew with the actual class name in each case, I am required to create 10 nearly identical implementations of Clone().

Is there a way to write one single Clone() method in the base class which will serve all 10 derived classes?

Edit: Is there a way to invoke the constructor of a class via one of it’s objects? In a way that will invoke the actual derived class constructor. Something like:

MyClass ^obj2 = obj1->Class->Construct(arg1, arg2);

I’m doing this on C++/CLI but answers from other languages are welcome.

  • 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-11T18:00:32+00:00Added an answer on May 11, 2026 at 6:00 pm

    In plain old C++, you can do this with compile-time polymorphism (the curiously-recurring template pattern). Assuming your derived classes are copyable, you can just write:

    
    class Base
    {
    public:
        virtual Base* Clone() const = 0;
    //etc.
    };
    template <typename Derived>
    class BaseHelper: public Base
    {
        //other base code here
    
        //This is a covariant return type, allowed in standard C++
        Derived * Clone() const
        {
             return new Derived(static_cast<Derived *>(*this));
        }
    };
    

    Then use it like:

    
    class MyClass: public BaseHelper<MyClass>
    {
        //MyClass automatically gets a Clone method with the right signature
    };
    

    Note that you can’t derive from a class again and have it work seamlessly – you have to “design in” the option to derive again by templating the intermediate classes, or start re-writing Clone again.

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

Sidebar

Related Questions

Here's my problem I have this javascript if (exchRate != ) { function roundthecon()
Here is what I want to do. Use this HTML line and have the
Here is what I am trying to achieve in PHP: I have this string:
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see
Here we go again, the old argument still arises... Would we better have a
I have a jquery bug and I've been looking for hours now, I can't
Here's an excerpt from java.text.CharacterIterator documentation: This interface defines a protocol for bidirectional iteration

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.