C++11——nullptr和NULL的區別

以往我們定義空指針都是

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中就是代表空指針,不能被轉換成數字

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章