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?


基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09