我的问题是 return;
和 C++ 中的 return NULL;
一样吗?
我理解在 C++ 中,return NULL;
在指针上下文中与 return 0;
相同。显然对于整数,情况并非如此,因为 NULL 不能被添加、减去等。并且一些人鼓励使用 0 而不是 NULL 作为指针,因为它更便于移植。我很好奇这是否是另一个发生等价的例子。
我怀疑它们是等价的,因为 return;
是说 return 'nothing' 而 NULL 是 'nothing'。但是,如果有人可以证实或否认这一点(当然要有解释),我将不胜感激!
最佳答案
is
return;
the same asreturn NULL;
in C++?
没有。
return
用于“中断”没有返回值的函数,即 void
的返回类型。
return NULL
返回值NULL
,它所在函数的返回类型必须与NULL
兼容。
I understand that in C++,
return NULL;
is the same asreturn 0;
in the context of pointers.
有点。 NULL
可能不等同于 0
,但它至少会转换为某种东西。
Obviously for integers, this is not the case as NULL cannot be added, subtracted, etc.
您可以很好地对指针执行加法和减法。但是,无论如何,NULL
必须具有整数类型(C++03 中的 4.10/1 和 18.1/4),因此它没有实际意义。 NULL
很可能是一个扩展为 0
或 0UL
的宏。
不过,如果您编写的实际上是 NULL
,一些现代编译器至少会警告您。
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.
没有。我不同意这个建议。虽然我可以看到它的来源,但由于 NULL
的确切定义因实现而异,使用 NULL
将使其更更容易替换为nullptr
当您切换到 C++11 时,如果没有其他内容是自记录的。
关于c++ - 在 C++ 中, "return;"是否与 "return NULL;"相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212901/