#include#include int main() { boost::scoped_ptr p(new int(5)); std::cout << *p << std::endl; // prints 5 p.reset(new int(10)); std::cout << *p << std::endl; // prints 10 return 0; }
#includeIn this example, a scoped_ptr `p` is created with a `MyClass` object. The `myFunction()` of the class is called using the pointer. The `reset` function is then used to delete the previous object and create a new one. Package library: Boost C++ Libraries#include class MyClass { public: MyClass() { std::cout << "MyClass constructor" << std::endl; } ~MyClass() { std::cout << "MyClass destructor" << std::endl; } void myFunction() { std::cout << "MyClass function" << std::endl; } }; int main() { boost::scoped_ptr p(new MyClass()); p->myFunction(); // calls the function of MyClass p.reset(new MyClass()); // releases the memory for the previous object and creates a new one return 0; }