当基类和派生类都继承自 boost::enable_shared_from_this 时,弱指针很糟糕

bad weak pointer when base and derived class both inherit from boost::enable_shared_from_this

本文关键字:this from 指针 shared enable 派生 基类 boost 继承      更新时间:2023-10-16

我有一个派生自 boost::enable_shared_from_this 的基类,然后是另一个派生自基类和 boost::enable_shared_from_this 的类:

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
class A : public enable_shared_from_this<A> { };
class B : public A , public enable_shared_from_this<B> {
public:
    using enable_shared_from_this<B>::shared_from_this;
};
int main() {
shared_ptr<B> b = shared_ptr<B>(new B());
shared_ptr<B> b_ = b->shared_from_this();
return 0;
}

这编译,但在运行时它给出

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
  what():  tr1::bad_weak_ptr
Aborted

是什么原因造成的,有没有办法解决它?

编辑:

如果我需要这样的东西怎么办:

class A : public enable_shared_from_this<A> { };
class B : public enable_shared_from_this<B> { };    
class C : public A, public B, public enable_shared_from_this<C> {
public:
    using enable_shared_from_this<C>::shared_from_this;
};
这样 A 和 B

都需要自己shared_from_this(一个不能从另一个继承),而 C 需要 A、B 和 shared_from_this?

在给定的继承链中,您不应该多次从enable_shared_from_this继承。

在这种情况下,你可以让基类A继承自enable_shared_from_this,并让派生类B返回一个shared_ptr<A>,然后将其static_pointer_castshared_ptr<B>

或者正如Omnifarious指出的那样,你可以在B中有一个函数为你做到这一点。 虽然,与其重载shared_from_this(),我更喜欢显式命名的函数,以尽量减少该类客户的意外:

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
class A : public boost::enable_shared_from_this<A> { };
class B : public A {
public:
    using enable_shared_from_this<A>::shared_from_this;
    shared_ptr<B> shared_B_from_this() {
        return boost::static_pointer_cast<B>(shared_from_this());
    }
    shared_ptr<B const> shared_B_from_this() const {
        return boost::static_pointer_cast<B const>(shared_from_this());
    }
};
int main() {
    shared_ptr<B> b = shared_ptr<B>(new B);
    shared_ptr<B> b1 = boost::static_pointer_cast<B>(b->shared_from_this());
    shared_ptr<B> b2 = b->shared_B_from_this();
    return 0;
}

以下是我解决您的问题的方法:

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
class virt_enable_shared_from_this :
   public enable_shared_from_this<virt_enable_shared_from_this>
{
 public:
   virtual ~virt_enable_shared_from_this() {}
};
template <class T>
class my_enable_shared_from_this : virtual public virt_enable_shared_from_this
{
 public:
   shared_ptr<T> shared_from_this() {
      return dynamic_pointer_cast<T>(virt_enable_shared_from_this::shared_from_this());
   }
};
class A : public my_enable_shared_from_this<A> { };
class B : public my_enable_shared_from_this<B> { };
class C : public A, public B, public my_enable_shared_from_this<C> {
 public:
   using my_enable_shared_from_this<C>::shared_from_this;
};
int main() {
   shared_ptr<C> c = shared_ptr<C>(new C());
   shared_ptr<C> c_ = c->shared_from_this();
   return 0;
}

这是痛苦的,至少有点丑陋。但在时尚之后,它的效果相当不错。我认为弗雷泽重新思考你的设计的想法可能是更好的选择。