nullptr

文章目录

1 nullptr

nullptr是C++11中新引入的关键字,主要用来表示控制针,数据类型为:std::nullptr_t。使用nullptr能够避免在整数和指针之间发生混淆。

首先看一下NULL和nullptr的类型:

cout << typeid(NULL).name() << endl;
cout << typeid(nullptr).name() << endl;

运行结果如下:
在这里插入图片描述
可以看到NULL和nullptr实际上是不同的类型。不过,对于指针的初始化以及指针有关的NULL场合,我们尽量使用nullptr替代NULL。

如果不替换,两者之间相互比较也是没有任何问题的:

int* p = null;

if (p == nullptr)
{
	cout << "p == nullptr" << endl;
}

参考资料:

  1. C++基础课

猜你喜欢

转载自blog.csdn.net/SlowIsFastLemon/article/details/106644131