Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当基类和派生类都继承自boost::enable_shared_from_this时,弱指针错误_C++_Boost_Shared Ptr_Multiple Inheritance_Enable Shared From This - Fatal编程技术网

C++ 当基类和派生类都继承自boost::enable_shared_from_this时,弱指针错误

C++ 当基类和派生类都继承自boost::enable_shared_from_this时,弱指针错误,c++,boost,shared-ptr,multiple-inheritance,enable-shared-from-this,C++,Boost,Shared Ptr,Multiple Inheritance,Enable Shared From This,我有一个从boost::enable_shared_this派生的基类,还有另一个从基类和boost::enable_shared_this派生的类: #include <boost/enable_shared_from_this.hpp> #include <boost/shared_ptr.hpp> using namespace boost; class A : public enable_shared_from_this<A> { }; clas

我有一个从boost::enable_shared_this派生的基类,还有另一个从基类和boost::enable_shared_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;
}
#包括
#包括
使用名称空间boost;
A类:从{}中公共启用{共享};
B类:公共A,公共启用共享{
公众:
使用enable_shared_from_this::shared_from_this;
};
int main(){
shared_ptr b=shared_ptr(新b());
shared_ptr b_ub=b->shared_from_this();
返回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
terminate在抛出“boost::exception\u detail::clone\u impl”的实例后调用
什么():tr1::坏的\u弱的\u ptr
流产
这是什么原因造成的,有什么办法可以解决吗

编辑:

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

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;
};
class A:公共启用\u共享\u来自\u this{};
B类:从{}中公共启用{u共享};
C类:公共A、公共B、公共启用共享{
公众:
使用enable_shared_from_this::shared_from_this;
};

这样,A和B都需要自己从中共享(并且一个不能从另一个继承),C需要A、B和从中共享?

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

在这种情况下,您可以将基类
A
保留为继承自
enable_shared_from_this
,并让派生类
B
将A
shared_ptr返回给
shared_ptr

或者正如Omnifarious所指出的,您可以在
B
中使用一个函数来为您实现这一点。尽管如此,我宁愿使用显式命名的函数,而不是从\u this()重载
shared\u,以尽量减少该类客户机的意外:

#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;
}
#包括
#包括
使用boost::shared\u ptr;
A类:public boost::从{u this}启用{u共享};
B类:公共A{
公众:
使用enable_shared_from_this::shared_from_this;
共享\u ptr从\u this()共享\u B_{
return boost::static_pointer_cast(来自_this()的共享_);
}
shared_ptr shared_B_from_this()常量{
return boost::static_pointer_cast(来自_this()的共享_);
}
};
int main(){
shared_ptr b=shared_ptr(新b);
shared_ptr b1=boost::static_pointer_cast(b->shared_from_this());
shared_ptr b2=b->shared_b_from_this();
返回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;
}
#包括
#包括
使用名称空间boost;
类virt\u从以下位置启用\u共享\u:
公共启用\u共享\u来自\u此
{
公众:
virtual~virt\u从\u this(){}启用\u共享\u
};
模板
类my_enable_shared_from_this:虚拟公共虚拟机启用_shared_from_this
{
公众:
共享\u ptr共享\u来自\u this(){
返回动态指针强制转换(virt_enable_shared_from_this::shared_from_this());
}
};

A类:公共我的\u启用\u共享\u来自\u这是重新思考您的设计的想法,可能是更好的选择。

我知道b与b相同,在这种情况下,我可以使用b。但这只是一个示范;在其他情况下,我实际上需要使用来自共享的\u,这将导致相同的错误。在这种情况下,我的解决方案将涉及虚拟继承。我怀疑boost可能已经有了一个用于此的类,可能是来自此
的非模板版本的
enable\u shared\u。但如果没有,我会创建自己的,并在需要功能的任何地方从中继承。当然,您必须使用
dynamic\u pointer\u cast
而不是
static\u pointer\u cast
。但它会起作用的。是的-我看不出一个明显干净的方法来让它起作用。这接近钻石继承,可能表明重新思考设计是正确的。或者更好的是,在派生类中重载共享___this为您执行演员阵容。我担心答案会是这样的。这里有一个稍微接近我实际拥有的情况:
class a:public enable\u shared\u from\u this{};B类:从{}中公共启用{u共享};C类:公共A、公共B、公共启用共享来自{public:using enable_shared_from_this::shared_from_this;}因此您可以看到A无法通过从B继承从_获得共享_(反之亦然)。那么,如何在所有的A、B和C中使用shared_from_?gah注释系统删除了新行,这使得它几乎无法阅读。。我也会编辑我的原始问题。我不明白从这个
继承虚拟
ly从这个
@MatthieuM>启用共享的目的。
从这个
启用共享的。
这个
是一个模板。因此,实际上从模板继承的每个人都不会有帮助,因为每个模板实例都是不同的
virt\u enable\u shared\u from\u这个
是给每个人一个公共基类,让他们从中虚拟继承。我想你可以用
static\u pointer\u cast
替换
dynamic\u pointer\u cast
在这里是安全的……而且faster@kassak:也许吧。我不想冒险。虚拟基类可以以有趣的方式在类布局中浮动。