C ++参考变量范围问题

时间:2012-09-22 02:11:55

标签: c++ scope destructor

我试图找出为什么要调用我的Tag类的析构函数

map<string, Tag>* TestLoader::loadCompoundTag()
{
    map<string, Tag>* compound = new map<string, Tag>();
    //Create locally scoped variable
    Tag tag;
    string tagName;
    do 
    {
        loadTag(tag, tagName);
            //Copies variable into map
        compound->insert(std::pair<string, Tag>(tagName, tag));
    //Tag destructor is called before debugger breaks on this line
    } while (tag.type != TAG_End);

    return compound;
}

void TestLoader::loadTag( Tag& tag, string& name )
{
    tag.i = 0;
    name = string("Test");
}

有人能告诉我为什么在那里调用析构函数吗?在循环的范围中没有定义任何变量,一个是在循环外创建的,另一个是在函数内创建的。谢谢!

2 个答案:

答案 0 :(得分:2)

Tag tag返回时,

loadCompoundTag()超出范围,当发生这种情况时,会调用它的析构函数。

答案 1 :(得分:2)

为了插入地图,你要创建一个临时的

std::pair<string, Tag>(tagName, tag)

在完整表达结束时,它被摧毁了。

你不应该担心。如有必要,可以使用emplace来避免,但不要担心。而是担心函数的结果类型:

为什么需要动态分配的地图?

我很确定你没有,也就是说,这是一种邪恶的过早优化。

因此,我强烈建议,关注正确性,让编译器完成优化工作,然后编写......

map<string, Tag> TestLoader::loadCompoundTag() const
{
    map<string, Tag> result;
    do 
    {
        Tag tag;
        string tagName;
        loadTag(tag, tagName);
        result->emplace( std::pair<string, Tag>( tagName, tag) );
    } while (tag.type != Tag::end);
    return result;
}

最有可能你甚至不需要让你的编译器进行优化,以便在这里得到返回值优化,这意味着显然本地result是在调用者提供的内存区域中构建的,这样就不会对功能结果进行复制。

相关问题