I am reading Scott Meyers \"Effective C++\" book. It was mentioned that there are tr1::shared_ptr
and tr1::weak_ptr
act like built-in pointers, but
For future readers.
Just want to point out that explanation given by Atom is excellent, here is working code
#include // and others
using namespace std;
class B; // forward declaration
// for clarity, add explicit destructor to see that they are not called
class A { public: shared_ptr b; ~A() {cout << "~A()" << endl; } };
class B { public: shared_ptr a; ~B() {cout << "~B()" << endl; } };
shared_ptr x(new A); //x->b share_ptr is default initialized
x->b = make_shared(); // you can't do "= new B" on shared_ptr
x->b->a = x;
cout << x.use_count() << endl;