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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:05:30+00:00 2026-05-16T20:05:30+00:00

I have seen that a useful way to write a clone method that returns

  • 0

I have seen that a useful way to write a clone method that returns a boost::shared_ptr is to do

class A
{
public:
  shared_ptr<A> Clone() const
  {
    return(shared_ptr<A>(CloneImpl()));
  }
protected:
  virtual A* CloneImpl() const
  {
    return(new A(*this));
  }
};

class B : public A
{
public:
  shared_ptr<B> Clone() const
  {
    return(shared_ptr<B>(CloneImpl()));
  }
protected:
  virtual B* CloneImpl() const
  {
    return(new B(*this));
  }
};

This allows the use of covariance with the regular pointer while still wrapping it in the safety of a smart pointer. My problem is my class B needs to inherit from boost::enable_shared_from_this because right after construction it needs to register itself with a separate class, passing a shared pointer to itself. I have a Create method that wraps construction and registration to make sure these always occur together. The above clone method implementation cannot handle this requirement, however. The registration cannot occur in CloneImpl since no shared_ptr yet exists “owning” the object, preventing a call to shared_from_this(), and if this logic is not in the virtual function then an shared_ptr that points to B, does not know about B’s registration needs when cloned. What is the best way to handle this problem?

  • 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-16T20:05:31+00:00Added an answer on May 16, 2026 at 8:05 pm

    Since you are already implementing the public interface covariance yourself via the non-virtual Clone() functions, you may consider abandoning the covariance for the CloneImpl() functions.

    If you only need shared_ptr and never the raw pointer, so you could then do:

    class X
    {
    public:
      shared_ptr<X> Clone() const
      {
        return CloneImpl();
      }
    private:
      virtual shared_ptr<X> CloneImpl() const
      {
        return(shared_ptr<X>(new X(*this)));
      }
    };
    
    class Y : public X
    {
    public:
      shared_ptr<Y> Clone() const
      {
        return(static_pointer_cast<Y, X>(CloneImpl())); // no need for dynamic_pointer_cast
      }
    private:
      virtual shared_ptr<X> CloneImpl() const
      {
        return shared_ptr<Y>(new Y(*this));
      }
    };
    

    CloneImpl() would always return a shared_ptr<Base> and now you could register your object inside the B::CloneImpl() function and return the registerd shared_ptr.

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

Sidebar

Related Questions

I have seen that BindableAttribute is used to decorate public properties in custom controls.
I am a newbie to Rails and I have seen that to write HTML
Possible Duplicate: Difference between Color.red and Color.RED I have seen that the Java class
I have seen that Windows system use temporary files to increase the performance of
I have seen that a prime number implementation of the GetHashCode function is being
I have seen that typing about:somekeywords in browsers address bar gives some information. What
I have seen that XGetKeyboardMapping() would let me do that, but the documentation is
There is a rotary control I have seen that is like the edge-on view
I have created VIEWS and PartialVIEWS, but so far, i have seen that VIEWS,
If you've used Google Wave or iGoogle you have probably seen that you can

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.