1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream> #include <memory> using namespace std; struct B { B(int i) : m(i) {} int m; }; struct A { shared_ptr<B> b; }; int main(void) { shared_ptr<B> x(new B(42)); A* a = new A; A* c = new A; a->b = x; c->b = x; cout << x.use_count() << endl; delete a; cout << x.use_count() << endl; delete c; cout << x.use_count() << endl; return 0; }