Dereferencing iterators

I have a list<int> LIST.
1
2
3
  list<int>::iterator listIter;
  for(listIter=LIST.begin();listIter!=LIST.end();listIter++)
  cout<< *listIter<<" ";

That code printed numbers in the list. Actually that's what I intended. But suddenly I realized that a list is much more than a sequnece of numbers. Where are those pointers to previous and next node. Why didn't they get printed? How does cout know not to print them?
operator* is defined in list::iterator in a way that it only returns the "value" or "data" contained in Node struct.

Think of Node struct implemented inside the list class that also defines iterator , const_iterator, reverse_iterator, and const_reverse_iterator.

This is NOT the actual implementation of std::list, but just to give you an idea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
template <typename Object>
class list
{
private:
    struct Node
    {
        Object data;
        Node* prev;
        Node* next;

        .
        .
        .
    }

    class iterator: public const_iterator
    {
        .
        .
        .
    }

    class const_iterator
    {
        Object & operator*( )
        {
            return current->data;
        }

        Node* current;

        .
        .
        .
    }

}


Last edited on
So a data structure contains our main data and also some auxiliary data required to maintain the structure itself. Dereferencing operator on an iterator is written in a way to return only the 'main' data.

I think I understood iterator invalidation also. As long as the structure of the data structure remains unchanged, iterators don't get invalidated.

Thanks for the reply
Topic archived. No new replies allowed.