Defined in header <memory>
|
||
template< class T > class shared_ptr;
|
(since C++11) | |
std::shared_ptr
is a smart pointer that retains shared ownership of an object through a pointer. Severalshared_ptr
objects may own the same object; the object is destroyed when the last remaining shared_ptr
pointing to it is destroyed or reset. The object is destroyed using delete-expression or a custom deleter that is supplied toshared_ptr
during construction.
A shared_ptr
may also own no objects, in which case it is called empty.
shared_ptr
meets the requirements of CopyConstructible
and CopyAssignable
.
Member types
Member type | Definition |
element_type | T |
Member functions
constructs new shared_ptr (public member function) |
|
destructs the owned object if no more shared_ptr s link to it (public member function) |
|
assigns the shared_ptr (public member function) |
|
Modifiers |
|
replaces the managed object (public member function) |
|
swaps the managed objects (public member function) |
|
Observers |
|
returns a pointer to the managed object (public member function) |
|
dereferences pointer to the managed object (public member function) |
|
returns the number of shared_ptr objects referring to the same managed object (public member function) |
|
checks whether the managed object is managed only by the current shared_ptr instance (public member function) |
|
checks if there is associated managed object (public member function) |
|
provides owner-based ordering of shared pointers (public member function) |
Non-member functions
creates a shared pointer that manages a new object (function template) |
|
creates a shared pointer that manages a new object allocated using an allocator (function template) |
|
applies static_cast, dynamic_cast or const_cast to the type of the managed object (function template) |
|
returns the deleter of specified type, if owned (function template) |
|
compares with another shared_ptr or with nullptr (function template) |
|
outputs the value of the managed pointer to an output stream (function template) |
|
(C++11)
|
specializes the std::swap algorithm (function template) |
specializes atomic operations (function template) |
Helper classes
(C++11)
|
hash support for std::shared_ptr (class template specialization) |
Implementation notes
In a typical implementation, std::shared_ptr holds only two pointers:
- a pointer to the referenced object
- a pointer to control block
Where the control block is a dynamically-allocated object that holds:
- a pointer to the managed object or the managed object itself
- the deleter
- the allocator
- the number of
shared_ptr
s that own the managed object - the number of
weak_ptr
s that refer to the managed object
When shared_ptr
is created by calling std::make_shared or std::allocate_shared, the control block holds the managed object directly, as a data member. When shared_ptr
is created by calling a constructor, a pointer is stored.
The pointer held by the shared_ptr
directly is the one returned by get()
, while the pointer/object held by the control block is the one that will be deleted when the number of shared owners reaches zero: these pointers are not necessarily equal.
The destructor of shared_ptr
decrements the number of shared owners of the control block, and if that reaches zero, the control block calls the destructor of the managed object, but the control block does not deallocate itself until thestd::weak_ptr counter reaches zero as well.
这个360还不让人复制,直接把JS禁掉,以前学的不是白学的