以前都是用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 }

nullptr

 

相关文章:

  • 2021-08-12
  • 2022-01-29
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
猜你喜欢
  • 2022-01-20
  • 2021-11-04
  • 2022-01-10
  • 2021-12-02
  • 2022-02-06
  • 2022-01-11
相关资源
相似解决方案