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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:00:42+00:00 2026-06-16T01:00:42+00:00

We have a Base class and a Derived class that derives from Base .

  • 0

We have a Base class and a Derived class that derives from Base.

In some other class, we want to have a member of type shared_ptr<Base>.

We cannot use the type Base directly because direct copying like that will rule out subclasses.

However, we still want to “copy” the Base (or subclass) object over upon construction, because we want to rule out the possibility of it being modified.

The classic way to deal with this is to put a virtual member function clone() into the Base class that every subclass of Base can then implement. Every clone() would then just return a “copy” of itself – for example, Derived would return make_shared<Derived>(*this).

The problem with this approach is that this requires every new subclass of Base to implement this clone() function. The code in each clone() is rather boilerplate, and it seems somewhat unnatural to be repeating it all the time.

Any better ways to do this since C++11?

  • 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-06-16T01:00:43+00:00Added an answer on June 16, 2026 at 1:00 am

    It has always been possible to do this in plain C++:

    struct base
    {
        virtual ~base () {}
        virtual base* clone () = 0;  
        virtual void foo () = 0;
    };
    
    template <typename T>
    struct base_impl : base
    {
        T* clone () { return new T (*static_cast<T*> (this)); }
    };
    
    struct derived : base_impl<derived>
    { 
        void foo () { ... }
    };
    
    struct derived2 : base_impl<derived2>
    {
        void foo () { ...}
    };
    

    etc.

    You can improve this with C++11: you can use unique_ptr<base> (but you lose the covariant return type), you can make the destructor of base_impl private and use friend T.

    I agree that this is not very flexible in this case but:

    • many layered hierarchies are not very common
    • clone functions are not very useful
    • the design is nevertheless extensible and goes beyond cloning: using templates as a way to automate boilerplate code is used thoroughly in eg. ATL and WTL. Search for “curiously recurring template pattern”.

    Another solution. This can probably be improved in various ways, but I think you can’t avoid having two clone functions:

    struct base
    {
        std::unique_ptr<base> clone () { return std::unique_ptr<base> (do_clone ()); }
    
    private:
        virtual base *do_clone () = 0;
    };
    
    template <typename T>
    struct base_impl : base
    {
        std::unique_ptr<T> clone () 
        {
            return std::unique_ptr<T> (static_cast<T*> (do_clone ()));
        }    
    
    private:
        base *do_clone () { return new T (*static_cast<T*> (this)); }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Derived that inherits directly from two base classes, Base1 and
I have a base class and several subclasses derived from that base class. I
I have a base and derived class. The base class constructors have some static
I have a c++ class derived from a base class in a framework. The
I have a base class which derives from boost::enable_shared_from_this, and then another class which
I have policies that derive from a base policy. Some classes specialize for the
I have a base class IStructure which is derived by many classes. Some of
I have a base class Base , that a lot of other classes will
I have multiple classes that inherit from one base class, and each class is
Scenario I have a single base class and 2 (possibly 3) other classes that

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.