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

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

问题描述


可能重复:

在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 comment from Stroustrup:


我应该使用NULL还是0?在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 ++ 0x中将其称为
的原因。然后,
nullptr将成为关键字。

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

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

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