enable_share_from_this

当类对象被 shared_ptr 管理时,需要在类自己定义的函数里把当前类对象作为参数传给其他函数时,这时需要传递一个 shared_ptr ,否则就不能保持 shared_ptr 管理这个类对象的语义(因为有一个 raw pointer 指向这个类对象,而 shared_ptr 对类对象的这个引用没有计数,很有可能 shared_ptr 已经把类对象资源释放了,而那个调用函数还在使用类对象——显然,这肯定会产生错误

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()

现在试一下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; 
}

运行结果:
[cpp] 
A::A() 
0 
A::~A()

在自己的类里面访问自己的成员,其实一点必要都没有,不过有一种可能,就是f函数需要返回自己的指针给调用者

class Y: public enable_shared_from_this<Y>
{
public:

    shared_ptr<Y> f()
    {
        return shared_from_this();
    }
}

int main()
{
    shared_ptr<Y> p(new Y);
    shared_ptr<Y> q = p->f();
    assert(p == q);
    assert(!(p < q || q < p)); // p and q must share ownership
}
时间: 2024-07-30 23:30:07

enable_share_from_this的相关文章

boost库----share_from_this类的作用和实现原理

使用boost库时,经常会看到如下的类 class A:public enable_share_from_this<A> 在什么情况下要使类A继承enable_share_from_this? 使用场合:当类A被share_ptr管理,且在类A的成员函数里需要把当前类对象作为参数传给其他函数时,就需要传递一个指向自身的share_ptr. 我们就使类A继承enable_share_from_this,然后通过其成员函数share_from_this()返回当指向自身的share_ptr. 以上

boost库share_from_this类的作用和实现原理

使用boost库时,经常会看到如下的类 class A:public enable_share_from_this<A> 在什么情况下要使类A继承enable_share_from_this? 使用场合 :当类A被share_ptr管理,且在类A的成员函数里需要把 当前类对象作为参数传给其他函数时,就需要传递一个指向自身的share_ptr. 我们就使类A继承enable_share_from_this,然后通过其成员函数 share_from_this()返回当指向自身的share_ptr.

如何用enable_shared_from_this 来得到指向自身的shared_ptr 及对enable_shared_from_this 的理解

在看<Linux多线程服务端编程:使用muduo C++网络库> 的时候,在说到如何防止在将对象的 this 指针作为返回值返回给了调用者时可能会造成的 core dump.需使用 enable_share_from_this. 首先要说明的一个问题是如何安全地将 this 指针返回给调用者.一般来说,我们不能直接将 this 指针返回.想象这样的情况,该函数将 this 指针返回到外部某个变量保存,然后这个对象自身已经析构了,但外部变量并不知道,那么此时如果外部变量使用这个指针,就会使得程序

C++学习之路: share_from_this&lt;T&gt;类的使用

引言: 当我们在类的内部需要定义一个指向 this 自身对象的 智能指针时, 会出现 两个同指向智能指针 分属两个系统的问题, 导致析构的时候 出现二次析构, 程序就会挂掉. 因为两个指针指向同一个对象,但是却不共享引用计数 那么在类内部如何获取 指向自身对象的 智能指针呢, 显式手动的获取很明显是错误的: 这时就需要在类 A 定义时继承一个 enable_share_from_this<A>  类即可 这样在类的内部就可以获取指向 自身的智能指针, 且影响其他智能指针的引用计数: 在类内部用

muduo 的windows下的编译

四处寻觅开源代码学习,适合的代码非常稀少,不适合的原因却千奇百怪. 不是使用语言特性过于老旧(c++03) 就是使用的冷僻语法(template<T> enable_share_from_this<T> 居然还搞个继承) 要么就是需要强大的系统背景知识(图形引擎,加密代码等) 或者无法但不调试,不能深入吸收. 辗辗转转,最后基本上也就筛选出这么几个优质代码 muduo  leveldb tinyhttp <深入应用C++11  代码优化与工程级应用>随书代码和作者的开源