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

查看:183
本文介绍了什么是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 [ ]如果将新项目添加到地图,表达式将返回引用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& operator [](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天全站免登陆