C++ Memory 库 - enable_shared_from_this
描述
它在派生类中启用 shared_from_this 成员函数。
声明
以下是 std::enable_shared_from_this 的声明。
template <class T> class enable_shared_from_this;
C++11
template <class T> class enable_shared_from_this;
参数
T − 这是一个指针类。
返回值
none
异常
noexcep − 它不会抛出任何异常。
示例
在下面的例子中解释了 std::enable_shared_from_this。
#include <iostream>
#include <memory>
struct C : std::enable_shared_from_this<C> { };
int main () {
std::shared_ptr<C> foo, bar;
foo = std::make_shared<C>();
bar = foo->shared_from_this();
if (!foo.owner_before(bar) && !bar.owner_before(foo))
std::cout << "bother pointers shared ownership";
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 −
bother pointers shared ownership

