以往我們定義空指針都是
int* p = NULL;
但是....NULL 在C++中被定義爲
於是有了以下試驗
#include <iostream> #include <sstream> #include <vector> using namespace std; void func(int* num) { cout << "ptr function" << endl; } void func(int num) { cout << "normal function" << endl; } int main() { func(NULL); func(nullptr);
return 0; }
猜猜輸出啥?
!!!
在編譯器解釋程序時,NULL會被直接解釋成0
根本不是我們所想的空
那要空怎麼辦!
nullptr!
nullptr在C++11中就是代表空指針,不能被轉換成數字