内存管理库
分配器
内存资源
未初始化存储 (C++20 前*)
垃圾收集器支持 (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 起)
(C++26 起为 constexpr)

构造新的 enable_shared_from_this 对象。值初始化 weak_this

参数

other - 要复制的 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 共享对象所有权
}

参阅

拥有共享对象所有权语义的智能指针
(类模板)