How to inherit and implement a pure virtual method with the abstract class as a parameter?

白昼怎懂夜的黑 提交于 2019-12-24 04:53:08

问题


I have an abstract class Node which contains a pure virtual method stub matches, requiring another instance of a Node (i.e. instance of something that subclasses Node) as a parameter.

class Node; // forward declaration

class Node {
public:
    Node() : parentNode(this) {}
    virtual ~Node() {}

    Node* parentNode;

    virtual bool matches(const Node& node) const = 0;
};

How can I implement matches in a subclass such that the parameter can be of the subclasses type as opposed to Node?

E.g. I want something like the following to register as the implemented version of the contract from Node, so that I can access NodeImpl specific properties as part of the function which I would otherwise be unable to do:

class NodeImpl : public Node {
private:
    int foo;
    ...
};

...

bool NodeImpl::matches(const NodeImpl& n) const {
    return this->foo == n.foo;
}

(I did have a try using templates to achieve this sort of effect, but I wasn't sure that I was doing it quite right. I found myself propagating the templates all over my code and encountering a myriad errors as such, and was hoping to get an idea of what the right method for this exactly is before I waste yet more time on what might well be also the wrong way of doing things.)

What I tried was:

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;
};

So that I could call matches generically in a template function like so:

template <class T>
void pathComp(Node<T>& currNode, Node<T>& rootNode) {
    Node<T> *node = &currNode;
    while (node->matches(rootNode)) {
        ...
    }
}

I couldn't quite get this method to work, plus I didn't like how I seemingly had to have class NodeImpl : public Node<NodeImpl> as my inheritance, something about that didn't seem quite right. Any advice as to whether I was on the right lines or not would be great!


回答1:


You can't really do that in general, because it wouldn't be type-safe. For example:

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!

The way you are talking about implementing it, there would be an assumption that b was the same type as a, but there is no way for the compiler to verify that assumption in general, so it isn't going to allow it.

However, if you are using two nodes of the same type, you can always have the matches() function just not be virtual:

struct Node {   }
struct NodeA : Node {  bool matches(const NodeA &) const; };

NodeA a1;
NodeA a2;
a1.matches(a2); // fine



回答2:


You should honor the superclass' contract signature. Then if you need to access sub-class properties, just cast to the sub-class, as needed.



来源:https://stackoverflow.com/questions/22419579/how-to-inherit-and-implement-a-pure-virtual-method-with-the-abstract-class-as-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!