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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:30:36+00:00 2026-05-25T11:30:36+00:00

I need to write a copy constructor that deep copies the contents of a

  • 0

I need to write a copy constructor that deep copies the contents of a std::shared_ptr. However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one.

Here is a code snippet with a comment that hopefully clarifies the issue.

class Foo {
public:
     Foo() {}
     Foo(Foo const & other);
     ...
private:
     int a, b, c, d, e;
     std::shared_ptr<Bla> p;
};

Foo::Foo(Foo const & other) {
    p.reset(new Bla(*other.p));

    // Can I avoid having to write the default copy constructor code below
    a = other.a;
    b = other.b;
    c = other.c;
    d = other.d;
    e = other.e;
}
  • 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-25T11:30:37+00:00Added an answer on May 25, 2026 at 11:30 am

    Here’s the question code as I’m writing this:

    class Foo {
    public:
         Foo() {}
         Foo(Foo const & other);
         ...
    private:
         int a, b, c, d, e;
         std::shared_ptr<Bla> p;
    };
    
    Foo::Foo(Foo const & other) {
        p.reset(new Bla(other.p));
    
        // Can I avoid having to write the default copy constructor code below
        a = other.a;
        b = other.b;
        c = other.c;
        d = other.d;
        e = other.e;
    }
    

    The above code is most likely wrong, because

    1. the default constructor leaves a, b, c, d and e uninitialized, and

    2. the code does not take charge of assignment copying, and

    3. the expression new Bla(other.p) requires that Bla has a constructor taking a std::shared_ptr<Bla>, which is extremely unlikely.

    With std::shared_ptr this would have to be C++11 code in order to be formally correct language-wise. However, I believe that it’s just code that uses what’s available with your compiler. And so I believe that the relevant C++ standard is C++98, with the technical corrections of the C++03 amendment.

    You can easily leverage the built-in (generated) copy initialization, even in C++98, e.g.

    namespace detail {
        struct AutoClonedBla {
            std::shared_ptr<Bla> p;
    
            AutoClonedBla( Bla* pNew ): p( pNew ) {}
    
            AutoClonedBla( AutoClonedBla const& other )
                : p( new Bla( *other.p ) )
            {}
    
            void swap( AutoClonedBla& other )
            {
                using std::swap;
                swap( p, other.p );
            }
    
            AutoClonedBla& operator=( AutoClonedBla other )
            {
                other.swap( *this );
                return *this;
            }
        };
    }
    
    class Foo {
    public:
         Foo(): a(), b(), c(), d(), e(), autoP( new Bla ) {}
         // Copy constructor generated by compiler, OK.
    
    private:
         int                      a, b, c, d, e;
         detail::AutoClonedBla    autoP;
    };
    

    Note that this code does initialize correctly in the default constructor, does take charge of copy assignment (employing the swap idiom for that), and does not require a special smart-pointer-aware Bla constructor, but instead just uses the ordinary Bla copy constructor to copy.

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

Sidebar

Related Questions

Please write a list of tasks that a copy constructor and assignment operator need
Need to write a post build event in VS 2010 project that copy just
Say I need a new type in my application, that consists of a std::vector<int>
I need to write a code that will copy one set of form values
I need to write a program that reads from stdin and only writes non-empty
We need to write a C/C++ code that will check whether the memory allocated
I need to write a String in a file on the client side, however
I need to write a bash script that will take a grepable nmap output
I often write something in gVim, then need to copy-paste it into another application.
How do I allow a class with a copy constructor that takes a non-const

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.