c++处理自引用的派生类
所以假设我在 c++ 中有一个像这样的树类
class Node{
void addChild(Node*);
/*obvious stuff*/
protected:
Node* parent;
vector<Node*> children
}
class specialNode : public Node{
void addChild(specialNode*);
/*obvious stuff*/
/*special stuff*/
}
现在每当我访问specialTree中的子级时,我显然会得到Node*,而不是specialNode*。
但是这个特殊的Node*具有Node所没有的成员变量和函数。
我可以强制specialNode只将specialNode作为子节点,否则会在编译时中断, 但在访问子/父时我仍然得到 Node* ,并且每当我想使用特殊函数时我都必须强制转换它,即使在特殊Node函数中也是如此。
有什么聪明的或者更好的方法来解决这个问题吗? 除了每次都直接铸造之外?
So suppose I have a tree class like this in c++
class Node{
void addChild(Node*);
/*obvious stuff*/
protected:
Node* parent;
vector<Node*> children
}
class specialNode : public Node{
void addChild(specialNode*);
/*obvious stuff*/
/*special stuff*/
}
Now whenever I access the children in specialTree, I obviously get Node*, not specialNode*.
But this specialNode* has member variables and functions that Node doesn't have.
I can force specialNode to only have take specialNode as children and otherwise break in compile time,
but I still get Node* when accessing children/parent, and I have to cast it whenever I want to use special functions, even in specialNode functions.
Is there any clever, or just any better way to go about this?
Other than literally casting every time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的树中只需要
SpecialNode
对象(并且只想将所有通用树功能封装在Node
中),则可以将Node
设为所谓的之后,您可以构造一个
SpecialNode
对象树,并使用SpecialNodeBase
中的所有方法以及来自Node 的其他树管理函数
If you only need
SpecialNode
objects in your tree (and just want to encapsulate all generic tree functionality inNode
) you can makeNode
a so called "mix-in" class likeAfter that you can construct a tree of
SpecialNode
objects and use all methods fromSpecialNodeBase
as well as additional tree-managing functions fromNode
因为子类中的 addChild 函数不是多态性,所以将其设为虚拟,但不允许跨基/子成员重载函数,因此我们必须更改子类中的 addChild 参数:
现在,它应该可以工作。
如果您想从子类(
specialNode
类)访问childeren
变量,则应该对其进行强制转换。例如:由于我们将 addChild 声明为虚函数,因此如果我们不确定
children[i]
,我们应该使用dynamic_cast
而不是static_cast
code> 始终是specialNode
类的实例,因此最好使用dynamic_cast
:Because addChild function in your child class is not polymorphism, make it virtual, but overloading functions across base/child members is not allowed, so we have to change the addChild parameter in the child class:
Now, it should work.
If you want to access to the
childeren
variable from the child class (specialNode
class), you should cast it. For example:Since we declared addChild as a virtual function, then we should use
dynamic_cast
instead ofstatic_cast
if we aren't sure thatchildren[i]
is always an instance ofspecialNode
class, and thus it is better to usedynamic_cast
:如果我理解正确,“混合”类解决方案将不允许您从
SpecialNodeBaseClass
实现的函数中调用addChild
。你实际上可以执行以下操作:
这可能看起来有点疯狂,但它在几个 GCC 版本上对我来说编译得很干净,所以我倾向于相信它并不是完全错误的。您现在应该能够从 Derived 内部调用 Base 的函数。
If I understand correctly, the "Mix-in" class solution won't allow you to call
addChild
from functions implemented bySpecialNodeBaseClass
.You can actually do the following:
This may look a little crazy, but it compiles cleanly for me on several GCC versions so I'm inclined to believe it's not totally wrong-headed. You should now be able to call the functions of Base from inside Derived.
在某些时候,您肯定必须将
Node *
转换为specialNode *
,但您可以通过仅在一处执行此操作来使其干净且易于管理。您可以添加一个成员函数,例如getParent
并在specialNode
中重写它,如下所示:当然,这是假设
specialNode
始终具有其他specialNode
作为父/子节点。如果你混合使用Node
和specialNode
,这显然是行不通的。You will definitely have to cast the
Node *
to aspecialNode *
at some point, but you can make this clean and easy to manage by doing this in only one place. You could add a member function, saygetParent
and override it inspecialNode
, like this:Of course, this is assuming that
specialNode
s always have otherspecialNode
s as parent/children. If you mixNode
s andspecialNode
s, this obviously won't work.