#include #include #include using namespace std; int main(int argc, char **argv) { // x contains a pointer to an int and has reference count 1. shared_ptr x(new int(10)); { // x and y now share the same pointer to an int, and they // share the reference count; the count is 2. shared_ptr y = x; cout << "y = " << y << " *y = " << *y << 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. cout << "x = " << x << " *x = " << *x << endl; // A preferred way to create a shared pointer (because it never // exposes a standard pointer): shared_ptr z = make_shared(100); cout << "z = " << z << " *z = " << *z << endl; return EXIT_SUCCESS; }