List iteration problem, it resets values
Hello!
Sorry for asking a probably stupid question, but I can't solve the problem. I'm writting some application and got in stuck with a part of it. Here I post a simplified part of it.
Code:
#include <list>
#include <iostream>
#include <conio.h>
using namespace std;
class Object {
private:
int value;
public:
Object(void);
int getValue(void);
void setValue(int value);
};
int main(void) {
Object one;
Object two;
list<Object> objects;
list<Object>::iterator i;
objects.push_back(one);
objects.push_back(two);
Object object;
for (i = objects.begin(); i != objects.end(); i++) {
object = *i;
object.setValue(9);
cout << object.getValue() << endl;
}
cout << "====" << endl;
for (i = objects.begin(); i != objects.end(); i++) {
object = *i;
cout << object.getValue() << endl;
}
getch();
return 0;
}
Object::Object(void) {
value = 0;
}
int Object::getValue(void) {
return value;
}
void Object::setValue(int value) {
this->value = value;
}
The output is:
In the first loop I change the values of 'value' property and output it immediately to check, then in the second loop I just output the values but they are zero again.