Namespaces
Variants

std::enable_shared_from_this<T>:: enable_shared_from_this

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
constexpr enable_shared_from_this ( ) noexcept ;
(1) (自 C++11 起)
enable_shared_from_this ( const enable_shared_from_this & other ) noexcept ;
(2) (自 C++11 起)

构造一个新的 enable_shared_from_this 对象。 weak_this 会被 值初始化

目录

参数

其他 - 复制一个 enable_shared_from_this

注解

不存在移动构造函数:从继承自 enable_shared_from_this 的对象进行移动操作不会转移其共享标识。

示例

#include <memory>
struct Foo : public std::enable_shared_from_this<Foo>
{
    Foo() {} // 隐式调用 enable_shared_from_this 构造函数
    std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
};
int main()
{
    std::shared_ptr<Foo> pf1(new Foo);
    auto pf2 = pf1->getFoo(); // 与 pf1 共享对象所有权
}

(C++11)
具有共享对象所有权语义的智能指针
(类模板)