在 C++ 中使用 NULL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3577580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:10:29  来源:igfitidea点击:

Using NULL in C++?

c++null

提问by Jookia

Possible Duplicate:
Do you use NULL or 0 (zero) for pointers in C++?

可能的重复:
您是否使用 NULL 或 0(零)作为 C++ 中的指针?

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

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

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

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

回答by Tarantula

In C++ NULL expands to 0 or 0L. See this comment from Stroustrup:

在 C++ 中,NULL 扩展为 0 或 0L。请参阅Stroustrup 的此评论:

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.

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.

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

如果必须命名空指针,则称其为 nullptr;这就是它在 C++0x 中的调用方式。然后,“nullptr”将是一个关键字。

回答by Jookia

The downside of NULL in C++ is that it is a define for 0. This is a value that can be silently converted to pointer, a bool value, a float/double, or an int.

C++ 中 NULL 的缺点是它是 0 的定义。这是一个可以静默转换为指针、bool 值、float/double 或 int 的值。

That is not very type safe and has lead to actual bugs in an application I worked on.

这不是非常类型安全,并且导致了我处理的应用程序中的实际错误。

Consider this:

考虑一下:

void Foo(int i);
void Foo(Bar* b);
void Foo(bool b);


main()
{
     Foo(0);         
     Foo(NULL); // same as Foo(0)
} 

C++11 defines a nullptrthat is convertible to a null pointer but not to other scalars. This is supported in all modern C++ compilers, including VC++ as of 2008. In older versions of GCC there is a similar feature, but then it was called __null.

C++11 定义了nullptr可转换为空指针但不能转换为其他标量的 a 。所有现代 C++ 编译器都支持这一点,包括 2008 年的 VC++。在旧版本的 GCC 中也有类似的功能,但后来被称为__null.

回答by Konrad

From crtdbg.h(and many other headers):

crtdbg.h(和许多其他头文件):

#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif

Therefore NULLis 0, at least on the Windows platform. So no, not that I know of.

因此NULL0,至少在Windows平台上。所以不,不是我所知道的。

回答by T.E.D.

I never use NULL in my C or C++ code. 0works just fine, as does if (ptrname). Any competent C or C++ programmer should know what those do.

我从不在我的 C 或 C++ 代码中使用 NULL。0工作得很好,就像if (ptrname). 任何有能力的 C 或 C++ 程序员都应该知道它们的作用。

回答by Mark B

Assuming that you don't have a library or system header that defines NULLas for example (void*)0or (char*)0it's fine. I always tend to use 0 myself as it is by definition the null pointer. In c++0x you'll have nullptravailable so the question won't matter as much anymore.

假设您没有定义NULL为例如的库或系统头文件,(void*)0或者(char*)0这很好。我自己总是倾向于使用 0,因为它是空指针的定义。在 c++0x 中,您将nullptr可用,因此问题不再重要。