c++ - 如何使 C++ 指针为空

标签 c++ pointers

<分区>

在声明所有 C++ 指针时,所有指针都应默认初始化为零或空值,以避免任何随机的不需要的值。这样我们就可以检查指针是否为空,这意味着未初始化。

谢谢

最佳答案

在 C++03 中,首选 ptr = 0 而不是 ptr = NULL(Bjarne S. 这么说。阅读下面引用自他的 site 的摘录)

在 C++0x 中,ptr = nullptr(参见 this)


Bjarne Stroustrup says that ,

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.

关于c++ - 如何使 C++ 指针为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5415115/

相关文章:

pointers - 如何在 Go 中强制将参数作为指针传递?

c - 声明动态多维指针

c++ - 函数返回后,指向字符串文字的指针是否仍然有效?

c++ - 在QT中改变QFrame的颜色

c++ - 使用来自 lldb : take 2 的字符串参数调用函数

c - 指针和双指针练习

c - 声明一个指向内存中地址 0x200 处的整数的指针

c++ - 如何在qt中获取QModelIndexList中的值?

c++ - 为什么不能使用显式模板参数调用模板 friend 功能?

c++ - 公开类的 boost::tuple 部分以 boost python