在 C++ 中使用 NULL? [英] Using NULL in C++?

查看:44
本文介绍了在 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆