码迷,mamicode.com
首页 > 其他好文 > 详细

初始化指针

时间:2015-04-12 09:21:16      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:学习笔记   visual c++ 2010   指针   

#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


初始化指针

标签:学习笔记   visual c++ 2010   指针   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!