From cplusplus.com:
Rarely you will come across a class that does not contain raw pointers yet the default copy constructor is not sufficient. An example of this is when you have a reference-counted object. boost::shared_ptr<> is example.
Can someone elaborate on this? If we have a class containing a boost::shared_ptr
, won't that get copy constructed when the class gets copy constructed - and hence won't the shared_ptr
constructor do the right thing and increase the reference count? The following code, for example, copies Inner
properly - why wouldn't this work for shared_ptr
?:
#include <iostream>
using namespace std;
class Inner
{
public:
Inner() { cout << "inner default constructed" << endl;}
Inner(const Inner& other) { cout << "inner properly copied" << endl;}
};
class Outer
{
Inner i;
};
int main() { Outer o; Outer p(o); return 0;}