问题描述
我有一个 SuperParent
类,父
类(源自 SuperParent
),并且两者都包含一个的shared_ptr
到儿童
类(其中包含一个的weak_ptr
到 SuperParent
)。不幸的是,试图将儿童的
指针时,我得到一个 bad_weak_ptr
例外。在code是以下内容:
的#include<升压/ enable_shared_from_this.hpp>
#包括LT&;升压/ make_shared.hpp>
#包括LT&;升压/ shared_ptr.hpp>
#包括LT&;升压/ weak_ptr.hpp>使用名字空间boost;类SuperParent;类儿童{
上市:
无效的setparent(shared_ptr的< SuperParent>母公司)
{
PARENT_ =父母;
}
私人的:
weak_ptr的< SuperParent> PARENT_;
};类SuperParent:公共enable_shared_from_this< SuperParent> {
保护:
无效InformChild(shared_ptr的<儿童>的孙子)
{
grandson->的setparent(shared_from_this());
grandson_ =孙子;
}
私人的:
shared_ptr的<儿童>孙子_;
};类家长:公共SuperParent,公共enable_shared_from_this<家长和GT; {
上市:
无效的init()
{
child_ = make_shared<儿童>();
InformChild(child_);
}
私人的:
shared_ptr的<儿童>儿童_;
};诠释的main()
{
shared_ptr的<家长和GT;父= make_shared<家长和GT;();
父 - >的init();
返回0;
}
这是因为你的父类继承enable_shared_from_this两次。
相反,你应该继承它一次 - 通过SuperParent。如果你希望能够得到的shared_ptr<家长>父类中,你可以从下面的辅助类也继承它:
模板<类派生>
类enable_shared_from_This
{
上市:
TYPEDEF提高:: shared_ptr的<衍生GT; PTR;PTR shared_from_This()
{
返回升压:: static_pointer_cast&所述;衍生GT;(的static_cast&下;衍生* GT(这) - > shared_from_this());
}
PTR shared_from_This()const的
{
返回升压:: static_pointer_cast&所述;衍生GT;(的static_cast&下;衍生* GT(这) - > shared_from_this());
}
};
然后,
类家长:公共SuperParent,公共enable_shared_from_This<家长和GT;
I have a SuperParent
class, a Parent
class (derived from SuperParent
) and both contain a shared_ptr
to a Child
class (which contains a weak_ptr
to a SuperParent
). Unfortunately, I'm getting a bad_weak_ptr
exception when trying to set the Child
's pointer. The code is the following:
#include <boost/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
using namespace boost;
class SuperParent;
class Child {
public:
void SetParent(shared_ptr<SuperParent> parent)
{
parent_ = parent;
}
private:
weak_ptr<SuperParent> parent_;
};
class SuperParent : public enable_shared_from_this<SuperParent> {
protected:
void InformChild(shared_ptr<Child> grandson)
{
grandson->SetParent(shared_from_this());
grandson_ = grandson;
}
private:
shared_ptr<Child> grandson_;
};
class Parent : public SuperParent, public enable_shared_from_this<Parent> {
public:
void Init()
{
child_ = make_shared<Child>();
InformChild(child_);
}
private:
shared_ptr<Child> child_;
};
int main()
{
shared_ptr<Parent> parent = make_shared<Parent>();
parent->Init();
return 0;
}
This is because your Parent class inherits enable_shared_from_this twice.Instead, you should inherit it once - through the SuperParent. And if you want to be able to get shared_ptr< Parent > within Parent class, you can inherit also it from the following helper class:
template<class Derived>
class enable_shared_from_This
{
public:
typedef boost::shared_ptr<Derived> Ptr;
Ptr shared_from_This()
{
return boost::static_pointer_cast<Derived>(static_cast<Derived *>(this)->shared_from_this());
}
Ptr shared_from_This() const
{
return boost::static_pointer_cast<Derived>(static_cast<Derived *>(this)->shared_from_this());
}
};
Then,
class Parent : public SuperParent, public enable_shared_from_This<Parent>
这篇关于bad_weak_ptr调用shared_from_this()在基类时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!