目录

enable_shared_from_this

描述 (Description)

它在派生类中启用shared_from_this成员函数。

声明 (Declaration)

以下是std :: enable_shared_from_this的声明。

template <class T> class enable_shared_from_this;

C++11

template <class T> class enable_shared_from_this;

参数 (Parameters)

T - 这是一个指针类。

返回值 (Return Value)

没有

异常 (Exceptions)

noexcep - 它不会抛出任何异常。

例子 (Example)

在下面的例子中解释了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 
↑回到顶部↑
WIKI教程 @2018