enable_share_from_this功能介绍

本文深入探讨了在C++中如何利用enable_shared_from_this来访问类的内部成员,特别是在成员函数内部通过this构造shared_ptr对象,并演示了这种实践的正确和错误使用场景。文章还通过对比实例,解释了enable_shared_from_this的作用及其在解决特定编程问题时的必要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个类很有意思,让一个被shared_ptr管理生命周期的类能够在自己的成员函数内部访问shared_ptr。有点绕。

举个例子,下面的代码在函数f内部通过this构造了shared_ptr对象,然后打印x_的值。

class B {
public:
    B(): x_(4) {
        cout << "B::B()" << endl;
    }
    
    ~B() {
        cout << "B::~B()" << endl;
    }
    
    void f() {
        shared_ptr<B> p(this);
        cout << p->x_ << endl;
        //shared_from_this();
    }
    
private:
    int x_;
};


/*
 * 
 */
int main(int argc, char** argv) {
    shared_ptr<B> x(new B);
    x->f();
    return 0;
}
编译通过,但是运行结果:

B::B()
4
B::~B()
B::~B()
两次析构B对象,这是个灾难。
现在试一下enable_shared_from_this:

class A : public enable_shared_from_this<A> {
public:
    A() {
        cout << "A::A()" << endl;
    }
    
    ~A() {
        cout << "A::~A()" << endl;
    }
    
    void f() {
        //cout << shared_from_this()->x_ << endl; // this way is okay too
        shared_ptr<A> p = shared_from_this();
        cout << p->x_ << endl;
    }
    
private:
    int x_;
};


/*
 * 
 */
int main(int argc, char** argv) {
    shared_ptr<A> x(new A);
    x->f();
    return 0;
}
运行结果:

A::A()
0
A::~A()

那么,为什么需要这样做呢?在自己的类里面访问自己的成员,其实只是个示例代码,一定必要都没有。

不过有一种可能,就是f函数需要返回自己的指针给调用者,难道这样写么?

A* f();
一个裸指针返回出去,失控了。谁也不知道调用者会干什么?

比较聪明的方法是设计成:

shared_ptr<A> f()

好了,这就是为什么我们需要enable_shared_from_this。


下面这篇文章介绍了enabled_shared_from_this几种错误使用:
http://hi.baidu.com/cpuramdisk/item/7c2f8d77385e0f29d7a89cf0



`enable_shared_from_this` 是 C++ 标准库提供的一个模板类,用于解决在某个类中获取自身的 `shared_ptr` 的问题。它的作用是允许一个对象在拥有 `shared_ptr` 的情况下,安全地获取到指向自身的 `shared_ptr`。 当一个类继承自 `enable_shared_from_this` 时,它就可以调用 `shared_from_this()` 成员函数来获取指向自身的 `shared_ptr`。这样可以确保在多个 `shared_ptr` 共享同一个对象时,不会因为对象被删除而导致悬空指针问题。 使用 `enable_shared_from_this` 的步骤如下: 1. 继承自 `enable_shared_from_this<T>`,其中 `T` 是派生类的类型。 2. 在需要获取自身的地方,调用 `shared_from_this()` 成员函数。 以下是一个示例代码: ```cpp #include <iostream> #include <memory> class MyClass : public std::enable_shared_from_this<MyClass> { public: std::shared_ptr<MyClass> getShared() { return shared_from_this(); } }; int main() { std::shared_ptr<MyClass> obj1 = std::make_shared<MyClass>(); std::shared_ptr<MyClass> obj2 = obj1->getShared(); std::cout << "obj1 use count: " << obj1.use_count() << std::endl; std::cout << "obj2 use count: " << obj2.use_count() << std::endl; return 0; } ``` 在这个示例中,`MyClass` 继承自 `enable_shared_from_this<MyClass>`,在 `getShared()` 函数中调用了 `shared_from_this()` 返回了一个指向自身的 `shared_ptr`。通过使用 `shared_ptr` 来管理对象的生命周期,可以确保在 `obj2` 存在时,`obj1` 不会被释放,从而避免了悬空指针问题。 需要注意的是,`enable_shared_from_this` 要求对象必须通过 `shared_ptr` 进行管理,否则会导致未定义行为。同时,不要在析构函数中使用 `shared_from_this()`,以避免潜在的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值