98用太久,学习下11,根据网上资料的知识总结。
1. nullptr是nullptr_t类型的右值常量,专门用于初始化空类型的指针
nullptr无法隐式转换为整型,而可以隐式匹配指针类型。
使用nullptr使程序更加健壮。
2. NULL是一个宏定义
VC中C++编译定义为: #define NULL 0
NULL隐式转换为整型。
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
3. 代码示例
class Test
{
public:
void DoTest(int i)
{
cout << "test:int" << endl;
}
void DoTest(int* i)
{
cout << "test:int*" << endl;
}
};
int main()
{
Test test;
test.DoTest(NULL);
test.DoTest(nullptr);
system("pause");
return 0;
}
运行结果: