shared_ptr enable_shared_from_this - C++11, 3 of n

#include <memory>
struct S: enable_shared_from_this<S>
{
};

Don't do this:
S *p = new S;
shared_ptr<S> sp2 = p->shared_from_this();  // throws std::bad_weak_ref

Do this:
shared_ptr<S> p(new S);
shared_ptr<S> sp2 = p->shared_from_this();

Rule: when a class inherits from enable_shared_from_this, it almost always means it should work with shared_ptr together.

What happened behind the scene for shared_ptr<S> p(new S) ?
(1) enable_shared_from_this construct is called
(2) S constructor is called
(3) shared_ptr constructor is called (the magic happens here)
      |
      | ->   __enable_shared_from_this_helper is called in shared_ptr constructor
                      |
                      |-> enable_shared_from_this::_M_weak_assign is called
                               |
                               |-> weak_ptr<_Tp>::._M_assign(__p, __n);
                                     (called on mutable enable_shared_from_this::_M_weak_this instance in
                                       struct S object. This calls sets (inits) the weak_ptr in struct S object)
                                         |
                                         |-> Set weak_ptr<_Tp>::_M_ptr and
                                                      weak_ptr<_Tp>:: _M_refcount (__weak_count<_Lp>)


Why the first case throws ?

// enabled_shared_from_this::shared_from_this
 __shared_ptr<_Tp, _Lp> shared_from_this()  { return __shared_ptr<const _Tp, _Lp>(this->_M_weak_this); }

// __shared_ptr constructor with weak_ptr
template<typename _Tp1>
explicit __shared_ptr(const __weak_ptr<_Tp1, _Lp>& __r) : _M_refcount(__r._M_refcount)// may throw
{
    __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
    // It is now safe to copy __r._M_ptr, as
    // _M_refcount(__r._M_refcount) did not throw.
    _M_ptr = __r._M_ptr;
}

// __share_count constructor with __weak_count
// Throw bad_weak_ptr when __r._M_get_use_count() == 0.
explicit __shared_count(const __weak_count<_Lp>& __r): _M_pi(__r._M_pi)
{
    if (_M_pi != 0)
        _M_pi->_M_add_ref_lock();
    else
        __throw_bad_weak_ptr();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值