以往我们定义空指针都是



int* p = NULL;


但是....NULL 在C++中被定义为

C++11——nullptr和NULL的区别_解释程序

 

 

 于是有了以下试验



#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

void func(int* num)
{
cout << "ptr function" << endl;
}

void func(int num)
{
cout << "normal function" << endl;
}

int main()
{
func(NULL);
func(nullptr);
return 0;
}


猜猜输出啥?

!!!

C++11——nullptr和NULL的区别_#include_02

 

 

 

在编译器解释程序时,NULL会被直接解释成0

根本不是我们所想的空

那要空怎么办!

nullptr!

nullptr在C++11中就是代表空指针,不能被转换成数字