本文介紹了C++ 指針的默認構造函數是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有這樣的代碼:
class MapIndex
{
private:
typedef std::map<std::string, MapIndex*> Container;
Container mapM;
public:
void add(std::list<std::string>& values)
{
if (values.empty()) // sanity check
return;
std::string s(*(values.begin()));
values.erase(values.begin());
if (values.empty())
return;
MapIndex *&mi = mapM[s]; // <- question about this line
if (!mi)
mi = new MapIndex();
mi->add(values);
}
}
我主要關心的是,如果將新項目添加到地圖中,mapM[s] 表達式是否會返回對 NULL 指針的引用?
The main concern I have is whether the mapM[s] expression would return reference to NULL pointer if new item is added to the map?
SGI 文檔是這樣說的:data_type&運算符[](const key_type& k)返回對與特定鍵關聯的對象的引用.如果地圖還沒有包含這樣的對象,operator[] 插入默認對象 data_type().
所以,我的問題是插入默認對象 data_type() 是否會創建一個 NULL 指針,或者它會創建一個指向內存中某處的無效指針?
So, my question is whether the insertion of default object data_type() will create a NULL pointer, or it could create an invalid pointer pointing somewhere in the memory?
推薦答案
它會創建一個 NULL
(0) 指針,無論如何它都是一個無效指針 :)
It'll create a NULL
(0) pointer, which is an invalid pointer anyway :)
這篇關于C++ 指針的默認構造函數是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!