nullptr

以前都是用0来表示空指针的,但由于0可以被隐式类型转换为整形,这就会存在一些问题。关键字nullptr是std::nullptr_t类型的值,用来指代空指针。nullptr和任何指针类型以及类成员指针类型的空值之间可以发生隐式类型转换,同样也可以隐式转换为bool型(取值为false)。但是不存在到整形的隐式类型转换

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int *p1 = NULL;
 7     int *p2 = nullptr;
 8     if (p1 == p2)
 9         cout << "same" << endl;
10     bool a = nullptr;
11     int b = NULL;
12     cout << "a:"<< a << endl;
13     cout << "b:" << b << endl;
14     //int c = nullptr;    //error
15 
16     system("pause");
17 }

 

posted on 2016-07-21 21:32  已停更  阅读(940)  评论(0编辑  收藏  举报