Thread: List iteration problem, it resets values

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    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:

    Code:
    9
    9
    ====
    0
    0
    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.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    object = *i;

    makes a copy of the object in the list. you modify and output the copy in the first loop and make and output a new copy in the second loop

    this one should do it:

    Code:
    for (i = objects.begin(); i != objects.end(); i++) {
                    i->setValue(9);
                    cout << i->getValue() << endl;
    }
    cout << "====" << endl;
    for (i = objects.begin(); i != objects.end(); i++) {
          cout << i->getValue() << endl;
    }
    also it would be better to minimize the scope of your vars, for example like that:
    Code:
    for ( list<Object>::iterator i = objects.begin(); i != objects.end(); i++) {
    instead of declaring i outside the for-loop. that could lead to unwanted sideeffects later
    Last edited by pheres; 11-30-2006 at 08:34 AM.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Quote Originally Posted by pheres
    object = *i;

    makes a copy of the object in the list. you modify and output the copy in the first loop and make and output a new copy in the second loop
    Thanks a lot!

    Although, by the moment I've realized this myself. But still thanks.


    Quote Originally Posted by pheres
    also it would be better to minimize the scope of your vars, for example like that:
    Code:
    for ( list<Object>::iterator i = objects.begin(); i != objects.end(); i++) {
    instead of declaring i outside the for-loop. that could lead to unwanted sideeffects later
    Thanks for your advice, but I'm usually careful with variables.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by Mike Borozdin
    I'm usually careful with variables.
    And that is exactly the reason why to limit their scope

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Ok, just like

    Code:
    int i
    in the 'for' loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Conflicting types of typedef error
    By advocation in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2005, 06:26 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM