std::shared_ptr for shared ownership An object accessed via std::shared_ptr s has its lifetime managed by those pointers through shared ownership. No specific std::shared_ptr owns the object. Instead all shared_ptr’s pointing to it collaborates to ensure its destruction at the point where it’s no longer needed. When the last shared_ptr pointing to an object stops pointing there, that shared_ptr destroys the object it points to. A shared_ptr can tell whether it’s the last one pointing to a resource by consulting the resource’s reference count, a value associated with the resource that keeps track of how many shared_ptrs point to it. Shared_ptrs constructors increments this count shared_ptr s destructors decrements it and copy assignment operators do both. If sp1 and sp2 are shared_ptr s to different objects, the assignment “ sp1=sp2 ” , modifies sp1 such that it points to the object pointed to by sp2 . sp1 reference count is decremented, while that for the obj...
Comments
Post a Comment