#includeint main() { boost::shared_ptr ptr(new int(42)); return 0; }
#includevoid foo(const boost::shared_ptr & ptr) { (*ptr)++; } int main() { boost::shared_ptr ptr(new int(42)); foo(ptr); return 0; }
#includeIn this example, we create a class that contains a shared_ptr to an integer object. The constructor initializes the shared_ptr with a new integer object with value 0. The class provides methods to set and get the value of the integer object. Since the class owns the shared_ptr, it is automatically deallocated when the class is destroyed. Overall, the RefPtr is a very useful class template for managing memory in C++. It is part of the Boost library, which is a collection of high-quality libraries for C++.class foo { public: foo() : bar_(new int(0)) {} void set_bar(int value) { *bar_ = value; } int get_bar() const { return *bar_; } private: boost::shared_ptr bar_; }; int main() { foo f; f.set_bar(42); return 0; }