#include#include "RefPtr.h" int main() { RefPtr p(new int(42)); std::cout << *p << std::endl; // prints 42 // now the reference count is 1 { RefPtr p2 = p; // now the reference count is 2 *p2 = 10; std::cout << *p << std::endl; // prints 10 } // now the reference count is 1 again return 0; }
#includeThis example creates a RefPtr object and initializes it with a dynamically allocated MyClass object. It then calls the AddRef() method to increment the reference count manually. It prints the reference count before and after the increment. It then calls the Release() method to decrement the reference count manually. It prints the reference count after the decrement. Package Library: Based on the header file name "RefPtr.h", it is likely that this class belongs to a custom library or framework.#include "RefPtr.h" class MyClass { public: MyClass() { std::cout << "MyClass()" << std::endl; } ~MyClass() { std::cout << "~MyClass()" << std::endl; } }; int main() { RefPtr p(new MyClass); std::cout << "Reference count: " << p.GetRefCount() << std::endl; // prints 1 p.AddRef(); std::cout << "Reference count: " << p.GetRefCount() << std::endl; // prints 2 p.Release(); std::cout << "Reference count: " << p.GetRefCount() << std::endl; // prints 1 return 0; }