问题:为什么C++17不可以打印字符指针变量为
nullptr
时的情况?
问题演示
char* str = nullptr;
int * x = nullptr;
cout << str; //不能打印
cout << x; //打印:000000
cout << nullptr; //打印:nullptr
问题:为什么C++17不可以打印字符指针变量为
nullptr
时的情况?
char* str = nullptr;
int * x = nullptr;
cout << str; //不能打印
cout << x; //打印:000000
cout << nullptr; //打印:nullptr
cout<< 其实是
cout.operator<<(T x),它对不同类型的 T 有不同的重载
当
cout<<char* 时会调用operator<<(char*)
cout<<int* 时会调用operator<<(const void*)
cout<<nullptr 会调用operator<<(nullptr)
所以结果不一样