C++ nil 与 NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1683608/
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-27 20:53:30  来源:igfitidea点击:

C++ nil vs NULL

c++null

提问by JT.

OK, I have some C++ code in a header that is declared like this:

好的,我在头文件中有一些 C++ 代码,声明如下:

void StreamOut(FxStream *stream,const FxChar *name = nil);

and I get: error:

我得到:错误:

'nil' was not declared in this scope

nil is a pascal thing, correct?

nil 是帕斯卡的东西,对吗?

Should I be using NULL?

我应该使用NULL吗?

I thought they were both the same or at least Zero, no?

我以为他们都是一样的,或者至少是零,不是吗?

回答by JaredPar

In C++ you need to use NULL, 0, or in some brand new compilers nullptr. The use of NULLvs. 0 can be a bit of a debate in some circles but IMHO, NULLis the more popular use over 0.

在 C++ 中,您需要使用NULL、0 或在一些全新的编译器中使用 nullptr。NULL与 0的使用在某些圈子中可能有点争论,但恕我直言,NULL是比 0 更流行的用法。

回答by Marcin

nildoes not exist in standard C++. Use NULLinstead.

nil在标准 C++ 中不存在。使用NULL来代替。

回答by notnoop

Yes. It's NULLin Cand C++, while it's nilin Objective-C.

是的。它NULLCand 中C++,而nil在 Objective-C 中。

Each language has its own identifier for no object. In Cthe standard library, NULLis a typedef of ((void *)0). In C++the standard library, NULLis a typedef of 0or 0L.

每种语言都有自己的标识符,没有对象。在C标准库中,NULL((void *)0). 在C++标准库中,NULL0or的 typedef 0L

However IMHO, you should never use 0 in place of NULL, as it helps the readability of the code, just like having constant variables in your code: without using NULL, the value 0 is used for null pointers as well as base index value in loops as well as counts/sizes for empty lists, it makes it harder to know which one is which. Also, it's easier to grepfor and such.

但是恕我直言,您永远不应该使用 0 代替NULL,因为它有助于代码的可读性,就像在您的代码中使用常量变量一样:如果不使用 NULL,则值 0 用于空指针以及循环中的基本索引值以及空列表的计数/大小,这使得更难知道哪个是哪个。此外,它更容易grepfor 等等。

回答by Guido

0 is the recommended and common style for C++

0 是 C++ 的推荐和常用风格

回答by friendzis

If you run a search through glibc you'll find this line of code:

如果您通过 glibc 进行搜索,您会发现这行代码:

#define NULL 0

It's just a standard way (not sure if it was published anywhere) of marking empty pointers. Variable value of 0 is still a value. Pointer pointing to 0 (0x0000... (it's decimal zero)) is actually pointing nowhere. It's just for readability.

这只是标记空指针的标准方式(不确定它是否在任何地方发布)。变量值为 0 仍然是一个值。指向 0 (0x0000 ... (它是十进制零)) 的指针实际上无处指向。这只是为了可读性。

int *var1, var2;
var1 = 0;
var2 = 0;

The above two assignments are not the same though they both look the same

以上两个作业虽然看起来一样,但并不相同

回答by Hyman

just add at the beginning

只需在开头添加

#define null '
#define nil '##代码##'
'

or whatever you want instead of nulland stick with what you prefer. The nullconcept in C++ is just related to a pointer pointing to nothing (0x0)..

或任何你想要的,而不是null坚持你喜欢的。C++ 中的概念只与一个指向的指针 ( 0x0..) 相关。

Mind that every compiler may have its own definition of null, nil, NULL, whatever.. but in the end it is still 0.

请注意,每个编译器可能都有自己的 null、nil、NULL 定义,等等……但最终它仍然是 0。

Probably in the source you are looking at there is a

可能在您正在查看的源代码中有一个

##代码##

somewhere in a header file..

在头文件的某个地方..

回答by Sean Copenhaver

I saw some comments on why not to use 0. Generally people don't like magic numbers, or numbers with meaning behind them. Give them a name. I would rather see ANSWER_TO_THE_ULTIMATE_QUESTION over 42 in code.

我看到了一些关于为什么不使用 0 的评论。一般来说,人们不喜欢神奇的数字,或者背后有含义的数字。给他们一个名字。我宁愿在代码中看到 ANSWER_TO_THE_ULTIMATE_QUESTION 超过 42。

As for nil, I know Obj-C using nil as well. I would hate to think that someone went against the very popular convention (or at least what I remember) of NULL, which I thought was in a standard library header somewhere. I haven't done C++ in awhile though.

至于 nil,我也知道 Obj-C 使用 nil。我不想认为有人违反了非常流行的 NULL 约定(或至少我记得的),我认为它在某个地方的标准库头文件中。不过我已经有一段时间没有使用 C++ 了。