【发布时间】:2013-10-27 15:16:25
【问题描述】:
我不得不简化一些代码来提出这个问题。但是,在下面的代码中,我没有将 x 声明为引用类型这一事实是否意味着一旦函数退出,我对递减的更改将被“遗忘”?
解决此问题的最明智方法是将x 声明为AnotherClass& x?
void MyClass::myFunc(unordered_map<int, AnotherClass>* dictionary, int z, int y){
AnotherClass x = dictionary->at(z);
//Does this change on x get "forgotten" in terms of what dictionary stores
//once myFunc() has finished, because x is not a reference/pointer type?
x.changeSomething(y--);
}
class MyClass{
public:
private:
myFunc(unordered_map<int, AnotherClass>* dictionary, int z);
unordered_map<int, AnotherClass>* dictionary
};
【问题讨论】:
标签: c++ pointers reference pass-by-reference