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中就是代表空指针,不能被转换成数字

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