問題描述
我有這樣的代碼:
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);
}
}
我主要關(guān)心的是,如果將新項目添加到地圖中,mapM[s] 表達(dá)式是否會返回對 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&運(yùn)算符[](const key_type& k)返回對與特定鍵關(guān)聯(lián)的對象的引用.如果地圖還沒有包含這樣的對象,operator[] 插入默認(rèn)對象 data_type().
所以,我的問題是插入默認(rèn)對象 data_type() 是否會創(chuàng)建一個 NULL 指針,或者它會創(chuàng)建一個指向內(nèi)存中某處的無效指針?
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?
推薦答案
它會創(chuàng)建一個 NULL
(0) 指針,無論如何它都是一個無效指針 :)
It'll create a NULL
(0) pointer, which is an invalid pointer anyway :)
這篇關(guān)于C++ 指針的默認(rèn)構(gòu)造函數(shù)是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!