#include // for EXIT_SUCCESS #include // for std::cout, std::endl #include // for std::shared_ptr int main(int argc, char** argv) { std::shared_ptr x(new int(10)); // x contains a pointer to an int and has reference count 1. std::cout << x.use_count() << std::endl; // temporary inner scope (!) { // x and y now share the same pointer to an int, and they // share the reference count; the count is 2. std::shared_ptr y(x); std::cout << y.use_count() << std::endl; } // y fell out of scope and was destroyed. Therefore, the // reference count, which was previously seen by both x and y, // but now is seen only by x, is decremented to 1. std::cout << x.use_count() << std::endl; std::cout << x.unique() << std::endl; return EXIT_SUCCESS; }