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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:46:29+00:00 2026-05-17T19:46:29+00:00

In the trivial example inheritance hierarchy: class Food { virtual ~Food(); }; class Fruit

  • 0

In the trivial example inheritance hierarchy:

class Food
{
    virtual ~Food();
};

class Fruit : public Food
{
    virtual ~Fruit();
};

class Apple: public Fruit
{
    virtual ~Apple();
}

class Vegetable: public Food
{
    virtual ~Vegetable();
}

I wish to create a method that can clone an object from its subclass or baseclass instance:

Apple* apple1 = new Apple();
Apple* clone1 = apple1->clone();

Food* food1 = apple1;
Apple* clone2 = food1->clone();

I see a few possible solutions to the problem:

  • Use polymorphism to create a virtual function that is explicitly defined for each subclass.

  • Use a templated factory method to take an instance of a class and the type of the subclass to create the new instance.

  • Register an id with each class and subclass that is stored elsewhere to lookup which subclass to create on calling a baseclass clone function.

None of these seem ideal, but I’m leaning more towards the third solution as it simplifies calling the clone function without requiring a definition to be written for every subclasses (for which there will be a lot).

However, I’m very much open to any suggestions, are there better ways of doing this?

  • 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-17T19:46:30+00:00Added an answer on May 17, 2026 at 7:46 pm

    You can use the CRTP to automatically implement a Clone method.

    template<typename T, typename Derive> class CloneImpl : public Derive {
    public:
        virtual Derive* clone() {
            return new T(static_cast<const T&>(*this));
        }
    };
    class Food {
    public:
        virtual Food* clone() = 0;
        virtual ~Food() {}
    };
    class Fruit : public Food {
    };
    class Dairy : public Food {
    };
    class Apple : public CloneImpl<Apple, Fruit> {
    };
    class Banana : public CloneImpl<Banana, Fruit> {
    };
    class Cheese : public CloneImpl<Cheese, Dairy> {
    };
    class Milk : public CloneImpl<Milk, Dairy> {
    };
    

    In this case, you can always call Clone() to copy the current object with a fresh allocation on the heap and you don’t need to implement it again in any class. Of course, if your Clone semantics need to be different, then you can just alter the function.

    Not only can the CRTP implement clone() for you, it can even do it between different inheritance hierarchies.

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

Sidebar

Related Questions

I have a class that needs access to urllib2, the trivial example for me
Consider classic virtual inheritance diamond hierarchy. I wonder to know what is the right
Why can't I use super to get a method of a class's superclass? Example:
What's a simple but non-trivial example of class defined using union in C++? the
could you explain me what is incorrect in this trivial example? class C1 (val
Consider this contrived, trivial example: var foo = new byte[] {246, 127}; var bar
A pretty silly trivial question. The canonical example is f = open('filename') , but
Probably a very trivial problem. I have an object that looks like this: @PersistenceCapable
A trivial example of an infinite IEnumerable would be IEnumerable<int> Numbers() { int i=0;
Consider this trivial example of fork() ing then waiting for a child to die

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.