如何以抽象类作为参数继承和实现纯虚方法
How to inherit and implement a pure virtual method with the abstract class as a parameter?
我有一个抽象类Node
,它包含一个纯虚拟方法存根matches
,需要Node
的另一个实例(即Node
子类的实例)作为参数。
class Node; // forward declaration
class Node {
public:
Node() : parentNode(this) {}
virtual ~Node() {}
Node* parentNode;
virtual bool matches(const Node& node) const = 0;
};
我如何在子类中实现matches
,使参数可以是子类类型而不是Node
?
。我想要以下内容注册为Node
的合同实现版本,这样我就可以访问NodeImpl
特定属性作为函数的一部分,否则我将无法这样做:
class NodeImpl : public Node {
private:
int foo;
...
};
...
bool NodeImpl::matches(const NodeImpl& n) const {
return this->foo == n.foo;
}
(我确实尝试过使用模板来实现这种效果,但我不确定我做得是否正确。我发现我自己在我的代码中传播模板,并遇到了无数的错误,并且希望在我浪费更多的时间在可能也是错误的做事方式之前得到一个正确的方法是什么。
我尝试的是:
template <class T>
class Node;
template <class T>
class Node {
public:
Node() : parentNode(this) {}
virtual ~Node() {}
Node* parentNode;
virtual bool matches(const T& node) const = 0;
};
这样我就可以在模板函数中调用match了,像这样:
template <class T>
void pathComp(Node<T>& currNode, Node<T>& rootNode) {
Node<T> *node = &currNode;
while (node->matches(rootNode)) {
...
}
}
我不能很好地让这个方法工作,加上我不喜欢我似乎必须把class NodeImpl : public Node<NodeImpl>
作为我的继承,这似乎不太对。任何关于我是否在正确的路线上的建议将是伟大的!
通常不能这样做,因为它不是类型安全的。例如:
struct Node { virtual bool matches(const Node &) const = 0; }
struct NodeA : Node { virtual bool matches(const NodeA &) const; };
struct NodeB : Node { virtual bool matches(const NodeB &) const; };
NodeA a; // compiler doesn't allow, but if it did...
NodeB b;
Node &c = a;
c.matches(b); // oops!
你谈论实现它的方式,会有一个假设,b
和a
是相同的类型,但是编译器没有办法验证这个假设,所以它不允许它。
但是,如果您使用的是两个相同类型的节点,则始终可以使matches()
函数不是虚拟的:
struct Node { }
struct NodeA : Node { bool matches(const NodeA &) const; };
NodeA a1;
NodeA a2;
a1.matches(a2); // fine
您应该遵守超类的合同签名。如果你需要要访问子类属性,只需根据需要转换为子类。
相关文章:
- 实现无开销push_back的最佳方法是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 这是实现图形的坏方法吗
- 在 c++ 中实现嵌套循环的更短方法吗?
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 如何在从抽象基派生的类中实现相同的方法?
- C++数组队列实现方法错误
- 有没有一种通用的方法来实现不变量
- 'using namespace'实现细节的便捷方法(仅标头库)?
- 使用非递归插入方法实现 AVL 树
- C ++类型特征:确保子类实现方法
- 实现基于数字值(正、负、零)的条件表达式的最佳方法
- 在C++上实现高斯赛德尔迭代方法
- C++方法实现:是否可以避免每次都键入类名?
- 如何知道C2259 VS 2017错误未实现哪种方法?
- C++强制在子类中实现方法,但具有不同的签名
- 避免在每个派生类中重新实现方法调用
- C :执行模板类型实现方法
- 在C++中实现方法时继承不起作用
- 还有更好的实现方法吗?