更新时间:2022-11-27 19:03:05
虽然我没有一般的答案或一些惯用法,有 boost :: enable_shared_from_this
。它允许您获得一个shared_ptr管理已经由shared_ptr管理的对象。因为在成员函数中,你没有引用那些管理shared_ptr的函数,enable_shared_ptr允许你获得一个shared_ptr实例,并在需要传递this指针时传递它。
While i don't have a general answer or some idiom, there is boost::enable_shared_from_this
. It allows you to get a shared_ptr managing an object that is already managed by shared_ptr. Since in a member function you have no reference to those managing shared_ptr's, enable_shared_ptr does allow you to get a shared_ptr instance and pass that when you need to pass the this pointer.
但这不会解决在构造函数中传递这个
的问题,因为那时, shared_ptr正在管理您的对象。
But this won't solve the issue of passing this
from within the constructor, since at that time, no shared_ptr is managing your object yet.