Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ 中不允许将 NULL 定义为 ((void*)0) #274

Closed
Mq-b opened this issue May 8, 2024 · 2 comments · Fixed by #279
Closed

C++ 中不允许将 NULL 定义为 ((void*)0) #274

Mq-b opened this issue May 8, 2024 · 2 comments · Fixed by #279

Comments

@Mq-b
Copy link
Contributor

Mq-b commented May 8, 2024

  • 文件路径:book/zh-cn/02-usability.md

nullptr 出现的目的是为了替代 NULL。在某种意义上来说,传统 C++ 会把 NULL、0 视为同一种东西,这取决于编译器如何定义 NULL,有些编译器会将 NULL 定义为 ((void*)0),有些则会直接将其定义为 0。

  • C 中,宏 NULL 可以拥有类型 void*,但这在 C++ 中不允许,因为空指针常量不能具有该类型。这是明确规定的。

应该改为

nullptr 出现的目的是为了替代 NULL。C++ 可能会把 NULL、0 视为同一种东西,这取决于编译器如何定义 NULL,宏 NULL 是由实现定义的空指针常量。而值为零的整数字面量就是空指针常量

@frederick-vs-ja
Copy link
Contributor

同时还存在的问题是 NULL 并不是编译器定义的。

@Mq-b
Copy link
Contributor Author

Mq-b commented May 12, 2024

区分标准库(如主流 libstdc++、MSVC STL、libc++)与编译器,对于不少人来说还是过于复杂了。很多表述的确不严谨,能修改自然更好。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants