C++零与空
好的,我在标头中有一些 C++ 代码,声明如下:
void StreamOut(FxStream *stream,const FxChar *name = nil);
我得到: error:
'nil' was not declared in this scope
nil is a pascal thing,对吗?
我应该使用 NULL 吗?
我认为它们都是相同的或者至少是零,不是吗?
OK, I have some C++ code in a header that is declared like this:
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?
Should I be using NULL?
I thought they were both the same or at least Zero, no?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 C++ 中,您需要使用 NULL、0,或者在某些全新的编译器中使用 nullptr。在某些圈子里,
NULL
与 0 的使用可能存在一些争论,但恕我直言,NULL
是比 0 更流行的用法。In C++ you need to use
NULL
, 0, or in some brand new compilers nullptr. The use ofNULL
vs. 0 can be a bit of a debate in some circles but IMHO,NULL
is the more popular use over 0.标准 C++ 中不存在
nil
。请改用NULL
。nil
does not exist in standard C++. UseNULL
instead.是的。在
C
和C++
中为NULL
,而在Objective-C 中为nil
。每种语言都有自己的对象标识符。在
C
标准库中,NULL
是((void *)0)
的typedef。在C++
标准库中,NULL
是0
或0L
的typedef。但是恕我直言,您永远不应该使用 0 代替
NULL
,因为它有助于提高代码的可读性,就像在代码中使用常量变量一样:如果不使用 NULL,值 0 将用于空指针以及循环中的基本索引值以及空列表的计数/大小,这使得更难知道哪个是哪个。此外,grep
等也更容易。Yes. It's
NULL
inC
andC++
, while it'snil
in Objective-C.Each language has its own identifier for no object. In
C
the standard library,NULL
is a typedef of((void *)0)
. InC++
the standard library,NULL
is a typedef of0
or0L
.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 togrep
for and such.0 是 C++ 推荐且常用的样式
0 is the recommended and common style for C++
如果您通过 glibc 进行搜索,您会发现这行代码:
这只是标记空指针的标准方法(不确定它是否已在任何地方发布)。变量值为 0 仍然是一个值。指向 0(0x0000...(十进制零))的指针实际上没有指向任何地方。这只是为了可读性。
上述两个作业虽然看起来相同,但并不相同
If you run a search through glibc you'll find this line of code:
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.
The above two assignments are not the same though they both look the same
只需在开头添加
或添加任何您想要的内容,而不是
null
并坚持您喜欢的内容。 C++ 中的 null 概念仅与指向任何内容的指针 (0x0
) 相关。请注意,每个编译器可能都有自己的 null、nil、NULL 等定义..但最终它仍然是0。
有一个地方..
可能在你正在查看的源代码中头文件中
just add at the beginning
or whatever you want instead of
null
and stick with what you prefer. The null concept in C++ is just related to a pointer pointing to nothing (0x0
)..Mind that every compiler may have its own definition of null, nil, NULL, whatever.. but in the end it is still 0.
Probably in the source you are looking at there is a
somewhere in a header file..
我看到一些关于为什么不使用 0 的评论。一般来说,人们不喜欢神奇的数字,或者背后有含义的数字。给他们起个名字。我宁愿在代码中看到 ANSWER_TO_THE_ULTIMATE_QUESTION 超过 42 个。
至于nil,我知道Obj-C也使用nil。我不愿意认为有人违反了 NULL 的非常流行的约定(或者至少是我记得的),我认为它位于标准库标头中的某个地方。不过我已经有一段时间没有接触C++了。
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.
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.