In C++, 'Node' is a class, 'node' is the instance of 'Node'. Why “ *node = nullptr ” is wrong, but “ *node = NULL” is correct?

  • A+
Node* node;     *node = nullptr; 

Report an error:

error: no viable overloaded '='        *node = nullptr; 


Node* node;     *node = NULL; 

is correct ?


NULL is most likely an old macro, inherited from the C world, and defined 0.

So what is happening in the second case is assigning the number 0 to a variable of type Node. Whereas in the first case a pointer (nullptr) is assigned. The compiler knows for sure that this is wrong, since *node is not a pointer. Which means it will complain.

Whether it will complain for assigning a numeric 0 to Node will depend if the assignment operator for Node has been overloaded to accept a number. Or whether there is an implicit conversion/constructor from a number to Node.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: