Definitions






Definitions

This section defines some terms used to explain the details of how shared_ptrs and weak_ptrs work.

A shared_ptr object owns a resource if it was

  • Contructed with a pointer to that resource

  • Constructed from a shared_ptr object that owns that resource

  • Constructed from a weak_ptr object that points to that resource

  • Assigned ownership of that resource either by operator= or by the member function reset; in this case, it no longer owns the resource, if any, that it owned before being assigned ownership of its new resource

A weak_ptr object points to a resource if it was

  • Constructed from a shared_ptr object that owns the resource

  • Constructed from a weak_ptr object that points to the resource

  • Assigned ownership of that resource by operator=; in this case, it no longer points to the resource, if any, that it pointed to before the assignment

More generally, an object controls a resource if it is a shared_ptr object that owns the resource or if it is a weak_ptr object that points to the resource.

An empty shared_ptr object does not own any resources. An empty weak_ptr object does not point to any resources.

Some functions assign control of a resource to an object. These functions are overloaded to obtain the resource from any of several sources; the properties of the object after the function returns depend on which version of the function was called. In the following sections, the names of the arguments are used to describe the source of the resource:

no arguments: the resulting object is empty.

ptr: a pointer of type Other* to the resource to be controlled. The type Ty must be a complete type. If the operation fails at runtimetypically by failing to allocate memoryit evaluates the expression delete ptr before exiting. If the operation succeeds, the resulting object is the original owner of the resource, and ptr is the original pointer to the resource.

ptr, dtor: a pointer of type Other* to the resource to be controlled and a deleter object for that resource. The type Ty must be a complete type. If it fails at runtimetypically by failing to allocate memorythe operation evaluates the expression dtor(ptr), which must be well defined, before exiting. See Section 2.10. If the operation succeeds, the resulting object is the original owner of the resource, and ptr is the original pointer to the resource.

sp: an object of type shared_ptr<Other>. The resulting object controls the resource that sp owns.

wp: an object of type weak_ptr<Other>. The resulting object controls the resource that wp points to.

ap: an object of type auto_ptr<Other> that holds a pointer to the resource to be controlled. If the operation succeeds, it calls ap.release(); otherwise, it leaves ap unchanged.

A shared_ptr object or a weak_ptr object releases control of a resource when the object is destroyed or when it is assigned control of a different resource.

When the last shared_ptr object that owns a resource releases control of that resource, the resource will be destroyed. When that happens, any remaining weak_ptr objects pointing to that resource have expired.

A controlled resource has a deleter if its original owner was assigned control of the resource by a function called with a pointer to the resource and a deleter object.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows