C++ enable_shared_from_this 解析

GCC源码

// enable_shared_from_this
template <typename _Tp>
class enable_shared_from_this {
 protected:
  constexpr enable_shared_from_this() noexcept {}

  enable_shared_from_this(const enable_shared_from_this&) noexcept {}

  enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept {
    return *this;
  }

  ~enable_shared_from_this() {}

 public:
  shared_ptr<_Tp> shared_from_this() {
    return shared_ptr<_Tp>(this->_M_weak_this);
  }

  shared_ptr<const _Tp> shared_from_this() const {
    return shared_ptr<const _Tp>(this->_M_weak_this);
  }

  weak_ptr<_Tp> weak_from_this() noexcept { return _M_weak_this; }

  weak_ptr<const _Tp> weak_from_this() const noexcept { return _M_weak_this; }

 private:
  // 注意:这个函数与 weak_ptr 与 shared_ptr 有着千丝万缕的联系
  // 作用应该是在某个合适的时机,将 _M_weak_this 绑定到 this 上
  template <typename _Tp1>
  void _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept {
    _M_weak_this._M_assign(__p, __n);
  }

  // Found by ADL when this is an associated class.
  friend const enable_shared_from_this* __expt_enable_shared_from_this_base(
      const enable_shared_from_this* __p) {
    return __p;
  }

  template <typename>
  friend class shared_ptr;

  mutable weak_ptr<_Tp> _M_weak_this;
};

用 weak_ptr 构造 shared_ptr :

// copy constructors (6) 
template <class U> explicit shared_ptr (const weak_ptr<U>& x);

C++ 文档解释该构造函数如下:

  • copy constructors (6)
    If x is not empty, the object shares ownership of x’s assets and increases the use count.
    If x is empty, an empty object is constructed (as if default-constructed).
  • copy from weak_ptr (7)
    Same as above (6), except that if x has expired, a bad_weak_ptr exception is thrown.
`std::enable_shared_from_this` 是一个模板类,其目的是为了解决在一个对象中保存 shared_ptr 的问题。它是 C++11 引入的一个特性。 在使用 `std::shared_ptr` 时,我们往往需要在对象中保存一个 `std::shared_ptr` 的副本,这样才能确保对象在使用完毕后不会被提前销毁。但是这种方式会导致一些问题,比如我们无法防止用户直接使用裸指针来操作对象,从而导致对象被提前销毁等问题。 这时候,我们可以使用 `std::enable_shared_from_this` 来解决这些问题。具体而言,我们需要继承 `std::enable_shared_from_this`,然后在对象中使用 `shared_from_this()` 方法来获取一个指向当前对象的 `std::shared_ptr`。 下面是一个示例代码: ```c++ #include <iostream> #include <memory> class MyClass : public std::enable_shared_from_this<MyClass> { public: std::shared_ptr<MyClass> get_shared_ptr() { return shared_from_this(); } }; int main() { std::shared_ptr<MyClass> p(new MyClass); std::shared_ptr<MyClass> q = p->get_shared_ptr(); std::cout << "p.use_count() = " << p.use_count() << std::endl; std::cout << "q.use_count() = " << q.use_count() << std::endl; return 0; } ``` 在这个示例中,我们定义了一个名为 `MyClass` 的类,并且继承了 `std::enable_shared_from_this`。然后,我们在类中定义了一个名为 `get_shared_ptr()` 的方法,该方法使用了 `shared_from_this()` 方法来获取一个指向当前对象的 `std::shared_ptr`。在 `main()` 函数中,我们先创建了一个 `std::shared_ptr` 对象 `p`,然后通过 `p` 调用 `get_shared_ptr()` 方法获取了一个指向同一个对象的 `std::shared_ptr` 对象 `q`。最后,我们输出了 `p` 和 `q` 的引用计数,可以看到它们的引用计数都是 2。 需要注意的是,在使用 `std::enable_shared_from_this` 时,我们需要确保对象已经被一个 `std::shared_ptr` 管理,否则使用 `shared_from_this()` 方法会导致程序崩溃。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值