C++ 指针的默认构造函数是什么? [英] What is the default constructor for C++ pointer?

查看:44
本文介绍了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++ 指针的默认构造函数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆