在 C++ 中使用 NULL?

2023-12-02C/C++开发问题
1

本文介绍了在 C++ 中使用 NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

可能的重复:
你使用 NULL 还是 0(零)表示C++中的指针?

在 C++ 中使用 NULL 还是仅使用值 0 是个好主意?

Is it a good idea to use NULL in C++ or just the value 0?

在从 C++ 调用的 C 代码中是否有使用 NULL 的特殊情况?喜欢 SDL?

Is there a special circumstance using NULL in C code calling from C++? Like SDL?

推荐答案

在 C++ 中,NULL 扩展为 0 或 0L.请参阅 引自 Stroustrup 常见问题解答:

In C++ NULL expands to 0 or 0L. See this quote from Stroustrup's FAQ:

在 C++ 中,NULL 的定义是 0,所以只有审美差异.我更喜欢避免使用宏,所以我使用 0.NULL 的另一个问题是人们有时会错误地认为它不同于 0 和/或不是整数.在前标准代码中,NULL 有时被/被定义为不合适的东西,因此必须/必须避免.现在这种情况不太常见了.

Should I use NULL or 0?

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.

如果一定要给空指针命名,就叫它nullptr;这就是它在 C++11 中的名称.然后,nullptr"将是一个关键字.

If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, "nullptr" will be a keyword.

这篇关于在 C++ 中使用 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6