C++11中enable_shared_from_this的用法解析

本文介绍了C++11中的`enable_shared_from_this`模板类,用于在类的成员函数中安全获取共享智能指针`std::shared_ptr`。内容包括`enable_shared_from_this`的作用、何时使用、如何使用以及注意事项,通过实例和源码分析帮助理解其工作原理。

什么是 enable_shared_from_this?

下面摘自 cpp reference 中概述

C++11 开始支持 enable_shared_from_this,它是一个模板类,定义在头文件 <memory>,其原型为:
template< class T > class enable_shared_from_this;

std::enable_shared_from_this 能让其一个对象(假设其名为 t ,且已被一个 std::shared_ptr 对象 pt 管理)安全地生成其他额外的 std::shared_ptr 实例(假设名为 pt1, pt2, … ),它们与 pt 共享对象 t 的所有权。
例如:若一个类 T 继承自 std::enable_shared_from_this<T> ,则 T 类中有继承自父类的成员函数: shared_from_this 。 当 T 类的对象 t 被一个为名为 pt 的 std::shared_ptr 类对象管理时,调用 T::shared_from_this 成员函数,将会返回一个新的 std::shared_ptr 对象,它与 pt 共享 t 的所有权。

为什么要用 enable_shared_from_this

  • 需要在类对象的内部中获得一个指向当前对象的 shared_ptr 对象。
  • 如果在一个程序中,对象内存的生命周期全部由智能指针来管理。在这种情况下,要在一个类的成员函数中,对外部返回 this 指针就成了一个很棘手的问题。

什么时候用?

当一个类被共享智能指针 share_ptr 管理,且在类的成员函数里需要把当前类对象作为参数传给其他函数时,这时就需要传递一个指向自身的 share_ptr

如何安全地将 this 指针返回给调用者?

一般来说,我们不能直接将 this 指针返回。如果函数将 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
      shared_ptr<_Tp>
      shared_from_this()
      {
   
    return shared_ptr
这段代码定义了一个名为 `RHStandardControllerServerImpl` 的类,它使用了 C++ 的多重继承特性。下面是对代码各部分的详细解析: ### 类定义与继承 ```cpp class RHStandardControllerServerImpl : public RHControllerServerImpl, public enable_shared_from_this<RHStandardControllerServerImpl> ``` - `class RHStandardControllerServerImpl`:这是定义一个新类的开始,`RHStandardControllerServerImpl` 是类的名称。 - `public RHControllerServerImpl`:表示 `RHStandardControllerServerImpl` 类公开继承自 `RHControllerServerImpl` 类。公开继承意味着基类 `RHControllerServerImpl` 的公有成员和保护成员在派生类 `RHStandardControllerServerImpl` 中保持其访问权限。派生类对象可以访问基类的公有和保护成员,并且可以将派生类对象当作基类对象使用(向上转型)。 - `public enable_shared_from_this<RHStandardControllerServerImpl>`:`enable_shared_from_this` 是 C++ 标准库中的一个模板类,位于 `<memory>` 头文件中。当一个类继承自 `enable_shared_from_this<T>`(这里 `T` 是该类本身,即 `RHStandardControllerServerImpl`)时,该类的对象可以在其成员函数内部安全地获取一个指向自身的 `std::shared_ptr`。这在需要将对象的所有权共享给其他部分时非常有用,避免了手动管理引用计数可能带来的问题。 ### 使用 `enable_shared_from_this` 的示例 ```cpp #include <iostream> #include <memory> class RHControllerServerImpl { public: void baseFunction() { std::cout << "Base function called." << std::endl; } }; class RHStandardControllerServerImpl : public RHControllerServerImpl, public std::enable_shared_from_this<RHStandardControllerServerImpl> { public: std::shared_ptr<RHStandardControllerServerImpl> getShared() { return shared_from_this(); } void derivedFunction() { std::cout << "Derived function called." << std::endl; } }; int main() { std::shared_ptr<RHStandardControllerServerImpl> obj = std::make_shared<RHStandardControllerServerImpl>(); std::shared_ptr<RHStandardControllerServerImpl> anotherObj = obj->getShared(); obj->baseFunction(); obj->derivedFunction(); return 0; } ``` 在这个示例中,`RHStandardControllerServerImpl` 类继承自 `RHControllerServerImpl` 和 `std::enable_shared_from_this<RHStandardControllerServerImpl>`。`getShared` 函数使用 `shared_from_this()` 方法返回一个指向当前对象的 `std::shared_ptr`。 ### 注意事项 - **对象必须由 `std::shared_ptr` 管理**:`enable_shared_from_this` 只能在对象已经被 `std::shared_ptr` 管理时使用。如果对象是直接创建的(例如 `RHStandardControllerServerImpl obj;`),调用 `shared_from_this()` 会导致未定义行为。
评论 12
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值