在 C++ 程序中,如果我想使用一个对任何错误都会返回 NULL 的 c 函数,我应该检查 nullptr
还是 NULL
?
int * ptr = someCfunction();
if (ptr == nullptr)
return false;
或者:
int * ptr = someCfunction();
if (ptr == NULL)
return false;
从风格上来说,哪个更好一些?
最佳答案
来自 nullptr 上的 cppreference ,您可以看到存在从 nullptr
到 NULL
的隐式转换。所以你可以做任何一个,但(作为好的做法)最好使用 nullptr
从 C++11 开始。
The keyword
nullptr
denotes the pointer literal. It is a prvalue of typestd::nullptr_t
. There exist implicit conversions fromnullptr
to null pointer value of any pointer type and any pointer to member type. Similar conversions exist for any null pointer constant, which includes values of typestd::nullptr_t
as well as the macroNULL
.
关于C++ 样式 : When using c function in c++, 我应该检查 NULL 还是 nullptr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53532697/