/* CSE 333 Su12 lecture 13 demo: sharedexample.cc */ /* Gribble/Perkins */ // boost shared_ptr. A shared_ptr maintains a reference count of the // number of shared_ptrs that refer to an object. The reference count // is adjusted on copys and assignments. The object is deleted when // no shared_ptrs refer to it (i.e., the reference count is // decremented to 0). #include #include #include int main(int argc, char **argv) { // x contains a pointer to an int and has reference count 1. boost::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. boost::shared_ptr y = x; std::cout << *y << 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 << std::endl; return EXIT_SUCCESS; }