Я пытаюсь создать личную переменную экземпляра и использовать метод getter, который возвращает ссылку на этот приватный ivar (я знаю, что могу просто сделать ivar общедоступным).
Когда я изменяю переменную после использования методов getter, кажется, что она изменяет копию, а ivar не оригинал.Есть идеи почему?
#include <iostream>
#include <tr1/unordered_map>
#include <tr1/functional>
#include <tr1/utility>
typedef std::tr1::unordered_map<std::string, std::string> umap_str_str;
class Parent {
public:
//add an item to the private ivar
void prepareIvar(bool useGetter)
{
std::pair<std::string, std::string> item("myKey" , "myValue");
if(useGetter){
//getting the reference and updating it doesn't work
umap_str_str umap = getPrivateIvar();
umap.insert( item );
}else {
//accessing the private ivar directly does work
_UMap.insert( item );
}
}
void printIvar()
{
std::cout << "printIvar\n";
for( auto it : _UMap){
std::cout << "\tKEY: " << it.first << "VALUE: " << it.second << std::endl;
}
}
//get a reference to the private ivar
umap_str_str& getPrivateIvar()
{
return _UMap;
}
private:
umap_str_str _UMap;
};
int main(int argc, const char * argv[])
{
Parent *p = new Parent();
p->prepareIvar(true);//use the getter first
p->printIvar();//it doesn't print the right info
p->prepareIvar(false);//access the private ivar directly
p->printIvar();//prints as expected
return 0;
}