my question is return; the same as return NULL; in C++?
I understand that in C++, return NULL; is the same as return 0; in the context of pointers. Obviously for integers, this is not the case as NULL cannot be added, subtracted, etc. And that it is encouraged by some to use 0 instead of NULL for pointers because it is more convenient for portability. I’m curious if this is another instance where an equivalence occurs.
I suspect that they are equivalent because return; is saying return ‘nothing’ and NULL is ‘nothing.’ However, if someone can either confirm or deny this (with explanation, of course), I would be very grateful!
No.
returnis used to “break” out from a function that has no return value, i.e. a return type ofvoid.return NULLreturns the valueNULL, and the return type of the function it’s found in must be compatible withNULL.Sort of.
NULLmay not be equivalent to0, but it will at least convert to something that is.You can perform addition and subtraction to pointers just fine. However,
NULLmust have integral type (4.10/1 and 18.1/4 in C++03) anyway so it’s moot.NULLmay very well be a macro that expands to0or0UL.Some modern compilers will at least warn you if it was actually
NULLyou wrote, though.No. And I disagree with this advice. Though I can see where it’s coming from, since
NULL‘s exact definition varies across implementations, usingNULLwill make it much easier to replace withnullptrwhen you switch to C++11, and if nothing else is self-documenting.