#include#include int main() { boost::shared_ptr ptr(new int(42)); int* raw_ptr = ptr.get(); std::cout << *raw_ptr << std::endl; // prints 42 *raw_ptr = 69; std::cout << *ptr << std::endl; // prints 69 return 0; }
#includeThis example creates a shared_ptr to a Foo object. It then obtains a raw pointer to the Foo object using the shared_ptr's get function. The shared_ptr is then reset, causing it to release ownership of the Foo object. However, the raw pointer is still alive and pointing to the Foo object on the heap. The raw pointer is then deleted, causing the Foo object to be destroyed.#include struct Foo { Foo() { std::cout << "Foo created." << std::endl; } ~Foo() { std::cout << "Foo destroyed." << std::endl; } }; int main() { boost::shared_ptr ptr(new Foo()); Foo* raw_ptr = ptr.get(); ptr.reset(); std::cout << "Raw pointer still alive." << std::endl; delete raw_ptr; std::cout << "Raw pointer deleted." << std::endl; return 0; }