C++ 样式 : When using c function in c++, 我应该检查 NULL 还是 nullptr?

标签 c++

在 C++ 程序中,如果我想使用一个对任何错误都会返回 NULL 的 c 函数,我应该检查 nullptr 还是 NULL

int * ptr = someCfunction();
if (ptr == nullptr)
    return false;

或者:

int * ptr = someCfunction();
if (ptr == NULL)
    return false;

从风格上来说,哪个更好一些?

最佳答案

来自 nullptr 上的 cppreference ,您可以看到存在从 nullptrNULL 的隐式转换。所以你可以做任何一个,但(作为好的做法)最好使用 nullptr 从 C++11 开始。

The keyword nullptr denotes the pointer literal. It is a prvalue of type std::nullptr_t. There exist implicit conversions from nullptr 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 type std::nullptr_t as well as the macro NULL.

关于C++ 样式 : When using c function in c++, 我应该检查 NULL 还是 nullptr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53532697/

相关文章:

c++ - 链表的数组实现

C++ SQLite 返回一些值?

c++ - 有没有办法修改这个 C++ 结构赋值 block 以直接在 C 中工作

c++ - 使用模式创建 2^n 个变量

c++ - 随机数生成器和线程安全

C++ NetLink 套接字错误

用于类比较的 C++ dynamic_cast vs typeid

c++ - C 是否有像 C++ 一样的定义规则?

c++ - 如何在 64 位版本的 Windows 中调用 SetWindowLong()?

c++ - 在 BST 中找到小于 K 的最大元素