std::enable_shared_from_this
来自cppreference.com
|
|
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
| 在头文件 <memory> 中定义
|
||
| template< class T > class enable_shared_from_this; |
(C++11 起) | |
std::enable_shared_from_this允许一个对象t目前由std::shared_ptr一个pt的安全产生额外的std::shared_ptrpt1, pt2, ...所有股份的所有权t的情况下,pt.原文:
std::enable_shared_from_this allows an object t that is currently managed by a std::shared_ptr named pt to safely generate additional std::shared_ptr instances pt1, pt2, ... that all share ownership of t with pt.继承
std::enable_shared_from_this<T>提供了一个类型的一个成员函数Tshared_from_this。如果一个对象t类型T管理的std::shared_ptr<T>命名pt,然后调用T::shared_from_this将返回一个新的std::shared_ptr<T>股份所有权的tpt.原文:
Inheriting from
std::enable_shared_from_this<T> provides a type T with a member function shared_from_this. If an object t of type T is managed by a std::shared_ptr<T> named pt, then calling T::shared_from_this will return a new std::shared_ptr<T> that shares ownership of t with pt.请注意,前呼叫一个目的
shared_from_thist,必须有一个std::shared_ptr拥有t.原文:
Note that prior to calling
shared_from_this on an object t, there must be a std::shared_ptr that owns t.此外,注意,
enable_shared_from_this提供了另一种std::shared_ptr<T>(this),这是可能导致this不止一次被破坏的海誓山盟,不知道由多个业主这样的表达式.原文:
Also note that
enable_shared_from_this provides an alternative to an expression like std::shared_ptr<T>(this), which is likely to result in this being destructed more than once by multiple owners that are unaware of eachother.目录 |
[编辑] 成员函数
| 构造一个 enabled_shared_from_this对象 原文: constructs an enabled_shared_from_this object (受保护的成员函数) | |
| 销毁的 enable_shared_from_this对象 原文: destroys an enable_shared_from_this object (受保护的成员函数) | |
| 返回一个参考this (受保护的成员函数) | |
| 返回一个shared_ptr共享所有权的*this 原文: returns a shared_ptr which shares ownership of *this (公共成员函数) | |
[编辑] 注释
enable_shared_from_this是保持一个弱引用(如std::weak_ptr),this共同实施。的构造函数std::shared_ptr可以检测到存在的一个enable_shared_from_this基地和股权与现有的std::shared_ptrs,而不是假设指针不管理任何人.原文:
A common implementation for
enable_shared_from_this is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr can detect presence of a enable_shared_from_this base and share ownership with the existing std::shared_ptrs, instead of assuming the pointer is not managed by anyone else.[编辑] 示例
#include <memory> #include <iostream> struct Good: std::enable_shared_from_this<Good> { std::shared_ptr<Good> getptr() { return shared_from_this(); } }; struct Bad { std::shared_ptr<Bad> getptr() { return std::shared_ptr<Bad>(this); } ~Bad() { std::cout << "Bad::~Bad() called\n"; } }; int main() { // Good: the two shared_ptr's share the same object std::shared_ptr<Good> gp1(new Good); std::shared_ptr<Good> gp2 = gp1->getptr(); std::cout << "gp2.use_count() = " << gp2.use_count() << '\n'; // Bad, each shared_ptr thinks it's the only owner of the object std::shared_ptr<Bad> bp1(new Bad); std::shared_ptr<Bad> bp2 = bp1->getptr(); std::cout << "bp2.use_count() = " << bp2.use_count() << '\n'; } // UB: double-delete of Bad
输出:
gp2.use_count() = 2 bp2.use_count() = 1 Bad::~Bad() called Bad::~Bad() called *** glibc detected *** ./test: double free or corruption
[编辑] 另请参阅
| (C++11) |
smart pointer with shared object ownership semantics (类模板) |