在使用空时,习惯这么赋值
int *p = NULL;

编译器进行解释程序时,NULL会被直接解释成0,所以这里的参数根本就不是大家所想的NULL,参数已经被编译器偷偷换成了0,0是整数。
因此这面的问题就尴尬了

让我们看下面的程序吧:

#include<iostream>
#include<string>
using namespace std;
void func(int* num)
{
	cout << "this is the ptr function..." << endl;
}

void func(int num)
{
	cout << "this is the normal function..." << endl;
}

int main()
{
	func(NULL);
	system("pause");
	return 0;
}

程序输出:
nullptr(C++11)和NULL的区别_#include

为啥呢不是this is the ptr function…这个。这就是C++中的一个缺陷。C++11的出现彻底解决了这个问题,nullptr在C++11中就是代表空指针,不能被转换成数字。

#include<iostream>
#include<string>
using namespace std;
void func(int* num)
{
	cout << "this is the ptr function..." << endl;
}

void func(int num)
{
	cout << "this is the normal function..." << endl;
}

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

nullptr(C++11)和NULL的区别_#include_02
想必大家看完这个代码理解了这两个的区别了吧!