首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::enable_shared_from_this

Defined in header <memory>

?

?

template< class T > class enable_shared_from_this;

?

(since C++11)

std::enable_shared_from_this允许对象t当前由std::shared_ptr命名pt安全地生成额外的std::shared_ptr实例pt1, pt2, ...所有股份的所有权t带着pt...

公开继承std::enable_shared_from_this<T>提供类型T具有成员函数shared_from_this.如果一个物体t类型Tstd::shared_ptr<T>命名pt,然后打电话T::shared_from_this将返回一个新的std::shared_ptr<T>拥有t带着pt...

成员函数

(constructor)

constructs an enabled_shared_from_this object (protected member function)

(destructor)

destroys an enable_shared_from_this object (protected member function)

operator=

returns a reference to this (protected member function)

shared_from_this

returns a shared_ptr which shares ownership of *this (public member function)

weak_from_this (C++17)

returns the weak_ptr which shares ownership of *this (public member function)

成员对象

Member name

Definition

weak_this (private)(C++17)

std::weak_ptr object tracking the control block of the first shared owner of *this. Exposition only

注记

的共同实现enable_shared_from_this保存弱引用%28,如std::weak_ptr%29至this...的建设者std::shared_ptr检测到enable_shared_from_this基并分配新创建的std::shared_ptr到内部存储的弱引用。构造一个std::shared_ptr对于已经由另一个对象管理的对象。std::shared_ptr不会查询内部存储的弱引用,从而导致未定义的行为。

它被允许打电话shared_from_this仅在以前共享的对象上,即在std::shared_ptr<T>。否则,在C++17%29之前,该行为是未定义的%28。std::bad_weak_ptr由共享引发%28。[医]来自默认构造的PTR构造函数weak_this%29%28自C++17%29。

enable_shared_from_this提供以下表达式的安全替代方案:std::shared_ptr<T>(this),这很可能导致this被多个不知道对方的所有者销毁不止一次,参见%29下面的示例。

二次

代码语言:javascript
复制
#include <memory>
#include <iostream>
 
struct Good: std::enable_shared_from_this<Good> // note: public inheritance
{
    std::shared_ptr<Good> getptr() {
        return shared_from_this();
    }
};
 
struct Bad
{
    std::shared_ptr<Bad> getptr() {
        return std::shared_ptr<Bad>(this);
    }
    ~Bad() { std::cout << "Bad::~Bad() called\n"; }
};
 
int main()
{
    // Good: the two shared_ptr's share the same object
    std::shared_ptr<Good> gp1 = std::make_shared<Good>();
    std::shared_ptr<Good> gp2 = gp1->getptr();
    std::cout << "gp2.use_count() = " << gp2.use_count() << '\n';
 
    // Bad: shared_from_this is called without having std::shared_ptr owning the caller 
    try {
        Good not_so_good;
        std::shared_ptr<Good> gp1 = not_so_good.getptr();
    } catch(std::bad_weak_ptr& e) {
        // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17)
        std::cout << e.what() << '\n';    
    }
 
    // Bad, each shared_ptr thinks it's the only owner of the object
    std::shared_ptr<Bad> bp1 = std::make_shared<Bad>();
    std::shared_ptr<Bad> bp2 = bp1->getptr();
    std::cout << "bp2.use_count() = " << bp2.use_count() << '\n';
} // UB: double-delete of Bad

二次

产出:

二次

代码语言:javascript
复制
gp2.use_count() = 2
bad_weak_ptr
bp2.use_count() = 1
Bad::~Bad() called
Bad::~Bad() called
*** glibc detected *** ./test: double free or corruption

二次

另见

shared_ptr (C++11)

smart pointer with shared object ownership semantics (class template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com