- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个摘要Node
类,派生于许多子类,例如Color
, Texture
, Shape
, Light
等...包含我的应用程序用户数据。数据包含在这些节点的大树中。每个Node
子类有固定数量的子类,它们是固定类型的。例如,一种 Material 可以有一个颜色子项和一个纹理子项。这些存储为 std::shared_ptr
的
目前,每个类派生一个setChild
方法,它需要一个 Node
作为论据。每个实现都使用 dynamic_cast
检查类型及其子类型。相应地,如果成功则设置它。
我想实现一个通用的 setChild
Node
中的方法,而不需要对其进行子类化。为此,每个子类都将在构造函数中声明(或注册)其子类,方法是提供一个名称字符串、一个类型字符串(对应于子类)和一个指向 shared_ptr
的指针。到 Node
.
你现在可以看到问题了:
**SubClass
, 向上转换为 **Node
,我知道这很糟糕,但由于每个子类都有一个 type()
方法来唯一标识类,并且由于我知道每个已注册子类的类型,因此我可以仔细检查以避免使用双指针存储错误的指针类型。**Node
来做这件事, 但带有 *std::shared_ptr<Node>
.在这里,我不确定是否做对了。问题:
shared_ptr<Subclass>
用*shared_ptr<Node>
即使我确定类型?谢谢,
艾蒂安
最佳答案
如果我理解你,以下是不安全的:
boost::shared_ptr<SpecificNode> realPtr;
boost::shared_ptr<Node>* castPtr;
// castPtr = &realPtr; -- invalid, cannot cast from one to the other
castPtr = reinterpret_cast<boost::shared_ptr<Node>*>(&castPtr);
castPtr->reset(anything); // illegal. castPtr does not point
// to a boost::shared_ptr<Node*>
现在你可能会很幸运,内存可能会排队,但这不是有效的 C++。
如果您正在寻找扩展第 3 方插件中的节点集,唯一的解决方案是一系列动态转换,所以让我们看看我们可以做些什么来使注册工作。
与其尝试使用强制转换来完成所有操作,不如考虑使用模板来执行类型安全操作。有一个接受节点共享指针的抽象基类,并且要么使用它,要么不使用它
(从现在开始,我将使用 T::Ptr 而不是 boost::shared_ptr,假设有 typedef。这只是为了便于 stackoverflow 阅读)
class Registration
{
public:
typedef boost::shared_ptr<Registration> Ptr;
virtual bool consume(const Node::Ptr&) = 0;
};
template <typename T>
class SpecificRegistration : public Registration
{
public:
SpecificRegistration(T::Ptr& inChildPtr)
: mChildPtr(inChildPtr)
{ }
virtual bool consume(const Node:Ptr& inNewValue)
{
if(!inNewValue) {
mChildPtr.reset();
return true; // consumed null ptr
} else {
T::Ptr newValue = dynamic_pointer_cast<T>(inNewValue);
if (newValue) {
mChildPtr = newValue;
return true; // consumed new value
} else {
return false; // no match
}
}
}
private:
T::Ptr& mChildPtr;
};
template <typename T>
Registration::Ptr registerChild(T::Ptr& inChildPtr)
{
return make_shared<SpecificRegistration<T> >(inChildPtr);
}
// you can also register vector<T::Ptr> if you write an
// ArraySpecificRegistration class which uses push_back when
// it consumes a node
void Node::setNode(const Node& inNode) {
for (RegistrationList::iterator iter = mRegistration.begin(); iter != mRegistration.end(); ++iter) {
if (mRegistration->consume(inNode))
return;
}
throw runtime_error("Failed to set any children of the node");
}
class SomeThirdPartyNode
: public Node
{
public:
SomeThirdPartyNode()
{
// all they have to write is one line, and mOtherTHing is
// registered
addChild(registerChild(mOtherThing));
}
private:
SomeOtherThirdPartyNode::Ptr mOtherThing;
};
关于c++ - 多态树类中的双指针向上转换(再次)、shared_ptr 和通用 setChild 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18585922/
使用 shared_ptr 时,我应该只使用 shared_ptr 吗?申报一次或申报shared_ptr无论我经过哪里? 所以在我新建实例的函数中,我将它包装在 shared_ptr 中但是当我从函
#include #include #include using namespace std; struct Node { Node(int data, boost::shared_pt
对于我目前正在处理的代码,我们有时需要使用较旧的编译器在一些较旧的系统上进行编译(例如,我们在较旧的 IBM BlueGene/L 上运行 sims,其支持契约(Contract)规定了一些非常旧的
我正在阅读 this answer作者指的是boost best practices其中说: Avoid using unnamed shared_ptr temporaries to save ty
我正在处理几个类,我想知道如何在我的应用程序类中使用普通成员,而该成员需要使用 shared_from_this()? 这里有一些代码来阐明我的意思(见评论) class Observable { p
我有一个 Foo 类,其中包含一个 Hotel 类 的 shared_ptr,以及一个 Rules 类(位于 namespace Rules 内): class Foo { public: //
我想不通。看起来我遗漏了一些简单的东西?我要在 MakePointToSameValue 中输入什么,以便在点 (1) b.ptr 和c.ptr 与a.ptr 指向同一个 换句话说,a.ptr.get
我已尽我所能制作了 SSCE。我怀疑共享指针在我在 main 中请求它们之前解构(释放)了我的对象。如何在不完全绕过共享指针的情况下防止这种情况发生?这是一个程序中的孤立问题,否则可以通过使用 sha
这个问题在这里已经有了答案: Set shared_ptr with new_pointer that is old_pointer + offset (1 个回答) 关闭 4 年前。 我目前正在学
假设我们有一个类,成员如下 std::map> member_我们无法替换 member通过具有 std::shared_ptr 的 map ,因为该类必须对 ObscureType 的非常量函数进行
我正在用 C++ 做学校作业(我还在学习)。我正在尝试实现随机生成的二叉树结构,使用 shared_ptr 在多个地方存储节点的信息(我需要它作为作业)。考虑以下示例代码(这是我的小测试程序): #i
我有以下类(class) struct Images { std::vector > ptr_vector; } 将 ptr_vector 放入 std::shared_ptr 中不会在复制
我刚刚对一个项目进行了大规模重构,添加了一个基类来代替现在所说基类的派生类(因为我想要这个类的更多“类型”)。 我的问题是,一些实用函数将原始类 A 的引用作为 shared_ptr,因此函数声明如下
我记得 Scott Meyers 教我的 func(shared_ptr(new P), shared_ptr(new Q)); 是危险的,因为(如果我没记错的话)内存分配、引用计数(构造)和分配给
给定 struct X { void f(std::shared_ptr); }; auto x(std::make_shared()); 我大概可以安全地做 x->f(std::move(x
我试着介绍了一些const一些新代码的正确性(实际上是功能范例),发现我无法传递 std::shared_ptr到一个需要 std::shared_ptr 的函数.请注意,我不想放弃 constnes
我需要将原始指针包装到 shared_ptr 中,以便将其传递给函数。该函数在返回后不保留对输入对象的任何引用。 { MyClass i; shared_ptr p(&i); f(p);
我在继承链中有4个类:A-> B-> C,A-> B-> D,其中B是唯一的类模板。 我想拥有一个在id和对象指针(C或D)之间映射的std::map,但是我在将make_shared输出分配给std
boost::shared_ptr 是否解决原始指针问题? Base* p = new Base(); shared_ptr sp(p); shared_ptr sq(p); 两个 shared_
How can shared_ptr be a subclass of shared_ptr? 我想知道如何实现模板类 C这样 C是 C 的子类? 我已经观察到上述情况,例如 shared_ptr和
我是一名优秀的程序员,十分优秀!