一 关于NULL、0、nullptr 1 在C语言中NULL被定义为:一个void* 指针,指向的地址为0。 1 #define NULL ((void *)0) 所以在C语言中我们通常会写出如下语句 1 2 int *i = NULL; foo_t *f = NULL; 2 而在C++中,NULL会被定义为0 1 2 3 4 5 #ifndef __cplusplus #define NULL ((void *)0) #else /* C++ */ #define NULL 0 #endif 3 C++11引入了nullptr 来表示空指针 二 在C++中使用NULL的风险 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 //func1 int mycall(char *a, char *b) { cout<<"char pointer!"<<endl; } //func2 int mycall(char *a, int b) { cout<<"int !"<<endl; } // func3 int mycall(char *a,nullptr_t nullp) { cout<<"nullptr !"<<endl; } int main() { char *a,*b; mycall(a,b); //char pointer! //优先调用func2,没有func2则调用func1或fu……

阅读全文