Using NULL in C++?(在 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中使用 NULL?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
