std::enable_shared_from_this::enable_shared_from_this

constexpr enable_shared_from_this() noexcept;
(1) (C++11 起)
enable_shared_from_this( const enable_shared_from_this<T>&obj ) noexcept;
(2) (C++11 起)

构造新的 enable_shared_from_this 对象。值初始化私有 std::weak_ptr<T> 成员。

目录

参数

obj - 要复制的 enable_shared_from_this

注意

无移动构造函数:从导出自 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)
拥有共享对象所有权语义的智能指针
(类模板)

版本历史

  • (当前 | 先前 2017年9月9日 (六) 07:43Fruderica讨论 | 贡献. . (1,156字节) (-447). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:56P12bot讨论 | 贡献 . . (1,603字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:02P12bot讨论 | 贡献 . . (1,603字节) (-85). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:24P12bot讨论 | 贡献 . . (1,688字节) (+529). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:25P12讨论 | 贡献 . . (1,159字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,159字节) (-34). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:09P12讨论 | 贡献 . . (1,193字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,193字节) (+1,193). . (Translated from the English version using Google Translate)