questions regarding shared_from_this

前端 未结 5 1838
野趣味
野趣味 2020-12-28 14:55

I have a function which takes a shared_ptr. In some member function memfun of MyClass, I need to pass this

5条回答
  •  北海茫月
    2020-12-28 15:30

    1) No, it's not impossible to do this without shared_from_this. You can simply construct a shared_ptr with a no-op deleter:

    void do_nothing(MyClass*) {}
    
    void MyClass:memfun()
    {
        func(shared_ptr(this, do_nothing));
    }
    

    Seeing as you don't actually seem to need shared_from_this after all, I'm going to skip the next two parts of your question.

提交回复
热议问题