Please take a look at this example:
#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;
myMainContainer tempInst;
tempInst.sub.val = "foo";
vec.push_back( tempInst );
tempInst.sub.val = "bar";
vec.push_back( tempInst );
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");
}
Hell, code for an example, I know.
Now you don’t need to think for many years about what this [should] [es]: I have a class myMainContainerthat has an instance as its only member mySubContainer. mySubContaineronly has a string valas a member.
So, I create a vector and fill it with some data samples.
Now what I want to do is: Iterate through the vector and create a separate function that can change the current myMainContainer in the vector. However, the vector remains unchanged as the message is displayed:
0: Value before='foo'
0: Value after='foo'
1: Value before='bar'
1: Value after='bar'
doSomethingWith void, myMainContainer, , , doSomethingWith .