[英]C++ override a member variable (std::vector)
here are the classes which my question is about 这是我的问题所涉及的课程
class Graph {}
class SceneGraph : public Graph {}
class Node {
public:
virtual Node* getNode(int index) { return mNodeList[index]; }
protected:
vector<Node*> mNodeList;
Graph* mGraph;
}
class TransformationNode : public Node {
public:
TransformationNode* getNode(int index) { return static_cast<TransformationNode*> (mNodelist[index]); }
void _update() {
auto beg = mNodeList.begin();
auto end = mNodeList.end();
while (begin != end) {
TransformationNode* node = static_cast<TransformationNode*> (*beg);
node->_update();
}
}
private:
bool mUpdated;
SceneGraph* mGraph;
}
First of all, I want to talk about the problems I solved. 首先,我想谈谈我解决的问题。 They may helps others.
他们可以帮助他人。 And you can confirm me if I am right ^^
如果我是对的,你可以确认我^^
Can I override a function with a different return type ? 我可以覆盖具有不同返回类型的函数吗?
Node* getNode(int index) became TransformationNode* getNode(int index) Node * getNode(int index)成为TransformationNode * getNode(int index)
Yes as long as return types are covariant : http://www.tolchz.net/?p=33
是的,只要返回类型是协变的: http : //www.tolchz.net/? p = 33
Can I override a member ? 我可以覆盖会员吗?
I don't know about overriding but a variable with the same name in the derived class will hide the one in the base class
我不知道覆盖,但派生类中具有相同名称的变量将隐藏基类中的变量
And there is the problem I really want to get around some how 还有一个问题我真的想要解决一些问题
In the TransformationNode class I did many (IMHO) avoidable type casting from base class to derived one. 在TransformationNode类中,我做了许多(恕我直言)可避免的类型转换,从基类到派生类。 I definitely know that all the elements in the mNodeList vector are TransformationNodes but to process mNodeList I have to type cast them.
我肯定知道mNodeList向量中的所有元素都是TransformationNodes但是为了处理mNodeList,我必须输入它们。
The inheritance is correct I mean TransformationNode is a Node 继承是正确的我意味着TransformationNode是一个Node
mNodeList holds child nodes of the node and it can not have a copy in the derived class which holds typecasted version of Nodes mNodeList保存节点的子节点,并且它不能在派生类中拥有一个副本,该类包含节点的版本化节点
And finaly I can even use reinterpered_cast if static_cast is more costly. 如果static_cast成本更高,我甚至可以使用reinterpered_cast。 Can you inform me about cost of these operations ?
你能告诉我这些操作的费用吗? are they really big performance issues ?
他们真的是性能问题吗?
assert (dynamic_cast)... kind of precaution has already been taken. 断言(dynamic_cast)......已经采取了一些预防措施。
briefly I want my compiler to know that mGraph is actually a SceneGraph* and mNodeList holds TransformationNode* this helps me to avoid lost of type casting. 简单地说,我希望我的编译器知道mGraph实际上是一个SceneGraph *并且mNodeList包含TransformationNode *这有助于我避免丢失类型转换。
Thank you for taking your time 感谢您抽出宝贵时间
1) is correct, you can indeed override (virtual!) base functions if the return type is more derived. 1)是正确的,如果返回类型更多派生,你确实可以覆盖(virtual!)基函数。
Ad 2): indeed, you cannot "override" members. 广告2):的确,你不能“覆盖”成员。 Redesign the base class if you need more flexible overridable behaviour.
如果需要更灵活的可覆盖行为,请重新设计基类。
static_cast
is a static operation that is resolved at compile time, so much like reinterpret_cast
it doesn't have any "cost". static_cast
是一个在编译时解析的静态操作,就像reinterpret_cast
一样,它没有任何“成本”。
As @Seth suggests in the comment, it might be an option to move the container. 正如@Seth在评论中建议的那样,移动容器可能是一种选择。 Ask yourself, can there ever be an abstract
Node
, or is every node actually of some derived concrete type? 问问自己,有没有抽象的
Node
,或者每个节点实际上是某些派生的具体类型? Perhaps you could make Node
abstract: 也许你可以使
Node
抽象:
struct Node { Node * getNode(size_t index) const = 0; };
struct TransformNode : Node
{
TransformNode * getNode(size_t index) const { return m_nodes[index]; }
private:
std::vector<TransformNode *> m_nodes;
};
Put the entire interface into the base class, but only implement it in each concrete class. 将整个接口放入基类,但只在每个具体类中实现它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.