std::shared_ptr
来自cppreference.com
| Defined in header <memory>
|
||
| template< class T > class shared_ptr; |
(C++11 起) | |
std::shared_ptr 是通过指针保持某个对象的共享拥有权的智能指针。若干个 shared_ptr 对象可以拥有同一个对象;最后一个指向该对象的 shared_ptr 被销毁或重置时,该对象被销毁。销毁该对象时使用的是 delete 表达式或者是在构造 shared_ptr 时传入的自定义删除器(deleter)。
shared_ptr 也可以不拥有对象,称作空(empty)。
shared_ptr 满足 CopyConstructible 和 CopyAssignable 的要求。
[编辑] 成员类型
| 成员类型 | 定义 |
| element_type | T |
[编辑] 成员函数
constructs new shared_ptr (公共成员函数) | |
| 解构拥有的对象如果没有更多 shared_ptrs的的链接 Original: destructs the owned object if no more shared_ptrs link to it The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 分配 shared_ptr Original: assigns the shared_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
Original: Modifiers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| 取代管理的对象 Original: replaces the managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 掉期所管理的对象 Original: swaps the managed objects The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
Original: Observers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |
| 返回一个指针,指向被管理对象 Original: returns a pointer to the managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 解引用指针到的管理对象 Original: dereferences pointer to the managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 返回 shared_ptr对象指的是在同一个管理对象的数量 Original: returns the number of shared_ptr objects referring to the same managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 检查是否被管理对象的管理仅由当前 shared_ptr的实例 Original: checks whether the managed object is managed only by the current shared_ptr instance The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 检查是否有相关的管理对象 Original: checks if there is associated managed object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
| 业主基于订购的共享指针 Original: provides owner-based ordering of shared pointers The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (公共成员函数) | |
[编辑] 非成员函数
| 创建一个共享管理一个新的对象的指针,该指针 Original: creates a shared pointer that manages a new object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 创建一个共享指针管理使用分配器分配一个新的对象 Original: creates a shared pointer that manages a new object allocated using an allocator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 适用于static_cast,dynamic_cast或const_cast被管理对象的类型 Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 返回指定类型的删除,如果拥有 Original: returns the deleter of specified type, if owned The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 与另一个 shared_ptr或nullptr Original: compares with another shared_ptr or with nullptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| 托管指针的值输出到输出流中 Original: outputs the value of the managed pointer to an output stream The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
| (C++11) |
专业的std::swap算法 Original: specializes the std::swap algorithm The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) |
| 专业原子操作 Original: specializes atomic operations The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (函数模板) | |
[编辑] Helper类
| (C++11) |
std::shared_ptr的哈希支持 Original: hash support for std::shared_ptr The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板专业化) |
[编辑] 实现说明
在典型的实现中,std::shared_ptr 只保存两个指针:
- 指向被管理对象的指针
- 指向控制块(control block)的指针
控制块是一个动态分配的对象,其中包含:
- 指向被管理对象的指针或被管理对象本身
- 删除器
- 分配器(allocator)
- 拥有被管理对象的
shared_ptr的数量 - 引用被管理对象的
weak_ptr的数量
通过 std::make_shared 和 std::allocate_shared 创建 shared_ptr 时,控制块将被管理对象本身作为其数据成员;而通过构造函数创建 shared_ptr 时则保存指针。
shared_ptr 持有的指针是通过 get() 返回的;而控制块所持有的指针/对象则是最终引用计数归零时会被删除的那个。两者并不一定相等。
shared_ptr 的析构函数会将控制块中的 shared_ptr 计数器减一,如果减至零,控制块就会调用被管理对象的析构函数。但控制块本身直到 std::weak_ptr 计数器同样归零时才会释放。