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;
}
Here’s the question code as I’m writing this:
The above code is most likely wrong, because
the default constructor leaves
a,b,c,dandeuninitialized, andthe code does not take charge of assignment copying, and
the expression
new Bla(other.p)requires thatBlahas a constructor taking astd::shared_ptr<Bla>, which is extremely unlikely.With
std::shared_ptrthis 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.
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
Blaconstructor, but instead just uses the ordinaryBlacopy constructor to copy.