我们都知道在C++11标准之前使用NULL对指针进行置空。但这是不严谨的使用方式。
因为
在C语言中
#define NULL ((void*)0)
并且支持
#define NULL 0
但在C++中
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
这是因为C++支持重载函数,若NULL可以定义为((void*)0)和0,则会在重载函数时造成二义性问题
void func(int a);
void func(char* b);
func(NULL); //ambiguity error
为此C++11中添加了nullptr关键字,它指代的是空指针类型,我们使用nullptr将改善我们上例的函数调用方式
void func(int a);
void func(char* b);
func(0); //func(int a);
func(nullptr) //func(char* b);
对于空指针的判断我们可以
if (ptr == nullptr)
if (ptr == 0)
if (ptr)