问题描述
我的问题是 return;
与C ++中的 return NULL;
相同吗?
my question is return;
the same as return NULL;
in C++?
我了解在C ++中,在指针的上下文中, return NULL;
与 return 0;
相同.显然,对于整数,不是这种情况,因为不能添加,减去NULL,等等.并且有人鼓励对指针使用0而不是NULL,因为它更便于移植.我很好奇这是否是另一个等效的情况.
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.
我怀疑它们是等效的,因为 return;
说的是return'nothing',而NULL是'nothing'.但是,如果有人可以确认或否认(当然有解释),我将不胜感激!
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!
推荐答案
否.
return
用于从没有返回值(即返回类型为 void
)的函数中突围".
return
is used to "break" out from a function that has no return value, i.e. a return type of void
.
return NULL
返回值 NULL
,并且找到的函数的返回类型必须与 NULL
兼容.
return NULL
returns the value NULL
, and the return type of the function it's found in must be compatible with NULL
.
排序. NULL
可能不等同于 0
,但至少会转换为.
Sort of. NULL
may not be equivalent to 0
, but it will at least convert to something that is.
您可以对指针执行加减运算.但是,无论如何, NULL
必须具有整数类型(在C ++ 03中为4.10/1和18.1/4),因此没有意义. NULL
可能是一个扩展为 0
或 0UL
的宏.
You can perform addition and subtraction to pointers just fine. However, NULL
must have integral type (4.10/1 and 18.1/4 in C++03) anyway so it's moot. NULL
may very well be a macro that expands to 0
or 0UL
.
但是,某些现代编译器至少会警告您,如果它实际上是您编写的 NULL
.
Some modern compilers will at least warn you if it was actually NULL
you wrote, though.
不.我不同意这个建议.尽管我可以看到它的来历,但由于 NULL
的确切定义在不同的实现中有所不同,因此使用 NULL
可以使它更容易替换为 nullptr
,当您切换到C ++ 11时,并且如果没有其他内容可自我说明.
No. And I disagree with this advice. Though I can see where it's coming from, since NULL
's exact definition varies across implementations, using NULL
will make it much easier to replace with nullptr
when you switch to C++11, and if nothing else is self-documenting.
这篇关于在C ++中,是"return;".与"return NULL;"相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!