一、为什么要有 nullptr 出现
nullptr 的出现是为了替代 NULL
那 NULL 为什么要被替代
那肯定 NULL 是有缺陷的
有的编译器将 NULL 定义成下面两种方式
1、0
2、((void*)0)
C++ 中不允许 ( void* ) 隐式的转成其他类型
所以,在某些编译器把 NULL 定义成 ((void*)0) 的情况下
当你定义变量去赋值NULL时候,NULL就会变定义为 0 。
另外,这种问题也会对 C++ 的重载特性造成混乱
所以 nullptr 来了
二、代码演示
*这里编写了MyClass类,里面有两个重载函数printf
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass() {}
~MyClass() {}
public:
void printf(char *) /*重载函数*/
{
cout << "This is char*" << endl;
}
void printf(int) /*重载函数*/
{
cout << "This is int" << endl;
}
};
int main(int argc, char **argv)
{
return 0;
}
接下来,我们 new 一个 MyClass 对