Professional Documents
Culture Documents
Is a smart pointer
Smart pointers are class objects
Behaves like built-in pointers
Magic is that it manages objects that you create with new
You don't have to worry about when and whether to delete them
Smart pointers automatically delete the object at appropriate time
Working mechanism
When the shared count is zero the managed object is destroyed by its
destructor call.
Object manager is destroyed when shared count and weak count both are
zero
Code example
std::shared_ptr<TestObject> obj1 = std::make_shared<TestChild>();
std::shared_ptr<TestChild> child1 = std::static_pointer_cast<TestChild>(obj1);
child1 = nullptr;
int test = 1;
if(test > 0) {
std::shared_ptr<TestObject> obj2 = obj1;
}
std::shared_ptr<TestObject> obj3 = obj1;
obj3.reset();
std::shared_ptr<TestObject> obj4 = obj1;
obj4 = nullptr;
obj3 = obj1;
std::weak_ptr<TestObject> wobj1(obj1);