首先要明白一点:
C语言中,NULL是指针,即#define NULL ((void *)0)
;
C++中NULL是0
C
由于C中的NULL是void*类型的指针,所以允许:
int *i = NULL;
foo_t *f = NULL;
void* 指针赋值给int *和foo_t *的指针的时候,隐式转换成相应的类型。而如果换做一个C++编译器来编译的话是要出错的,因为C++是强类型的,void *是不能隐式转换成其他指针类型的,所以通常情况下,编译器提供的头文件会这样定义NULL:
#ifdef __cplusplus ---简称:cpp c++ 文件
#define NULL 0
#else
#define NULL ((void *)0)
#endif
C++
在C++中,由于无法隐式转换,所以引入了0来表示NULL。但NULL==0并不能解决重载问题,有时候程序运行的结果会和预想中的不一样,为了解决二义性问题,C++11引入了nullptr。
#include <iostream>
using namespace std;
void func(void* i){
cout << "func1" << endl;
}
void func(int i){
cout << "func2" << endl;
}
int main(){
func(NULL);//可能调用fun2
func(nullptr);//调用fun1
return 0;
}
在没有C++ 11的nullptr的时候,是如何解决重载问题的?《Imperfect C++》中提示是:
const class nullptr_t
{
public:
template<class T>
inline operator T*() const
{ return 0; }
template<class C, class T>
inline operator T C::*() const
{ return 0; }
private:
void operator&() const;
} nullptr = {};
以上通过模板类和运算符重载的方式来对不同类型的指针进行实例化从而解决了(void*)指针带来参数类型不明的问题,另外由于nullptr是明确的指针类型,所以不会与整形变量相混淆。