首页 > 其他 > 详细

初始化指针

时间:2015-04-12 09:19:25      阅读:232      评论:0      收藏:0      [点我收藏+]
#include<iostream>
using namespace std;
int main()
{
	int *a(nullptr), *b(NULL), *c(0);
	if(!a) cout<<" 'a' does not point to anything. \n";
	if(!b) cout<<" 'b' does not point to anything. \n";
	if(!c) cout<<" 'c' does not point to anything. \n";
	return 0;
}

nullptr C++新标准引入的特性, Visual C++ 2010 编译器支持它。过去已经使用0NULL(编译器将用0代替此宏)来初始化指针,当然它们现在仍然可以使用。但是,使用nullptr初始化指针要好得多。

因为字面值nullptr可以隐式转换为bool类型,所以我们可以如下面这样来检查指针的状态:

if(!a) 

cout<<" ‘a‘ does not point to anything. \n"; 

nullptr转换为boolfalse,其他任何指针值都可以为true


初始化指针

原文:http://blog.csdn.net/noob_f/article/details/44998847

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!