2

I'm writing an interface for a method.

void method(Node* node);

The interface has the code

class Node {
 public:
     virtual void init(Node* a) = 0;
};

The sub class has the code

class CNode: public Node {
 public:
     void init(Node* a);
     void init(CNode* a);
}

In the code CNode::init(Node* a), the function will try to convert a into CNode, then call CNode::init(CNode* a).

I'm trying to implement it with

void CNode::init(Node *a) {
    CNode b = dynamic_cast<CNode *>(*a);
}

However, clang reported this error

'Node' is not a pointer

How can I solve this problem?

vize_xpft
  • 67
  • 5
  • 3
    It should be : `CNode* b = dynamic_cast(a);` – Vuwox May 20 '19 at 13:25
  • 2
    You can only use `dynamic_cast` on pointers, by dereferencing the ptr (`*a`) you're trying to `dynamic_cast` an object, which is what the error is saying – xEric_xD May 20 '19 at 13:25

1 Answers1

6

It should be:

void CNode::init(Node *a) {
    if (CNode *b = dynamic_cast<CNode *>(a))
        init(b);
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436