C++零与空

发布于 2024-08-10 14:48:40 字数 286 浏览 3 评论 0原文

好的,我在标头中有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

预谋 2024-08-17 14:48:40

在 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 of NULL vs. 0 can be a bit of a debate in some circles but IMHO, NULL is the more popular use over 0.

方圜几里 2024-08-17 14:48:40

标准 C++ 中不存在 nil。请改用NULL

nil does not exist in standard C++. Use NULL instead.

温柔少女心 2024-08-17 14:48:40

是的。在CC++ 中为NULL,而在Objective-C 中为nil

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

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

Yes. It's NULL in C and C++, while it's nil in Objective-C.

Each language has its own identifier for no object. In C the standard library, NULL is a typedef of ((void *)0). In C++ the standard library, NULL is a typedef of 0 or 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 grep for and such.

凤舞天涯 2024-08-17 14:48:40

0 是 C++ 推荐且常用的样式

0 is the recommended and common style for C++

離殇 2024-08-17 14:48:40

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

#define NULL 0

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

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

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

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

#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.

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

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

余厌 2024-08-17 14:48:40

只需在开头添加

#define null '\0'

或添加任何您想要的内容,而不是 null 并坚持您喜欢的内容。 C++ 中的 null 概念仅与指向任何内容的指针 (0x0) 相关。

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

有一个地方..

#define nil '\0'

可能在你正在查看的源代码中头文件中

just add at the beginning

#define null '\0'

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

#define nil '\0'

somewhere in a header file..

简美 2024-08-17 14:48:40

我看到一些关于为什么不使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文