【发布时间】:2011-10-16 17:40:33
【问题描述】:
我觉得这个问题足够基本,可以在某个地方找到,但我似乎无法找到答案。
假设我有这个代码:
//class member function
std::map< std::string, std::string > myMap;
const std::map< std::string, std::string >& bar()
{
return myMap;
}
void myFunc( std::map< std::string, std::string >& foo1 )
{
foo1 = bar();
std::map< std::string, std::string >& foo2 = bar();
}
我的理解是,如果我开始使用 foo2,因为 foo2 是对与 bar() 返回的相同实例的引用,所以我对 foo2 所做的任何事情都会反映在 myMap 中。但是 foo1 呢? foo1 是否获得了 myMap 的副本,或者它是否也指向与 bar() 返回的实例相同的实例? c++ 标准库说 std::map 的赋值运算符会将元素复制过来,但这是否意味着在 foo2 的声明中并未真正调用赋值运算符?
谢谢!
【问题讨论】:
标签: c++ reference assignment-operator