请看看这个例子:C++向量指针/引用问题
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class mySubContainer
{
public:
string val;
};
class myMainContainer
{
public:
mySubContainer sub;
};
void doSomethingWith(myMainContainer &container)
{
container.sub.val = "I was modified";
}
int main()
{
vector<myMainContainer> vec;
/**
* Add test data
*/
myMainContainer tempInst;
tempInst.sub.val = "foo";
vec.push_back(tempInst);
tempInst.sub.val = "bar";
vec.push_back(tempInst);
// 1000 lines of random code here
int i;
int size = vec.size();
myMainContainer current;
for(i = 0; i < size; i ++)
{
cout << i << ": Value before='" << vec.at(i).sub.val << "'" << endl;
current = vec.at(i);
doSomethingWith(current);
cout << i << ": Value after='" << vec.at(i).sub.val << "'" << endl;
}
system("pause");//i suck
}
甲地狱大量的代码为一个例子,我知道。
现在,您不必花费多年的时间考虑这个[应该]做什么[es]:我有一个班级myMainContainer
,其中有一个mySubContainer
的实例。 mySubContainer
只有一个字符串val
作为成员。
因此,我创建一个向量并填充一些示例数据。
现在,我想要做的是:遍历向量并创建一个单独的函数,以便能够修改向量中的当前myMainContainer。但是,向量保持不变的输出告诉:
0: Value before='foo'
0: Value after='foo'
1: Value before='bar'
1: Value after='bar'
- 我在做什么错?
doSomethingWith必须返回void,我不能让它返回修改后的myMainContainer
,然后就覆盖它的载体,这就是为什么我试图通过参考上文中doSomethingWith定义视为通过。