C++入门-nullptr与NULL
int* p1 = NULL;
int* p2 = 0;
int* p3 = nullptr;
p1和p2在c++中相同,在c语言中p1是(void *)0。(通过下面的定义可以看出)
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
所以在c++下要使调用时的参数为指针必须使用nullptr。
void f(int)
{
cout << "f(int)" << endl;
}
void f(int*)
{
cout << "f(int*)" << endl;
}
int main()
{
f(0);//结果输出f(int)
f(NULL);//结果输出f(int)
f((int*)NULL);//结果输出f(int*)
return 0;
}