Tools.h++ Manual
104011 Tandem Computers Incorporated 12-9
12
The rule is "advance and then return." All Tools.h++ iterators work this way.
1
If you change the collection by adding or deleting objects while an iterator is
active, the state of the iterator also becomes undefined—using it could bring
unpredictable results. The member function
reset()
will restart the iterator,
as if it had just been constructed.
At any given moment the iterator "marks" an object in the collection. You can
think of it as the "current" object. There are various methods for moving this
"mark."
Most of the time you will probably be using member function
operator()
. It
is designed to always advance to the next object, then either return
TRUE
or a
pointer to the next object, depending on whether the associated collection is
value-based or reference-based, respectively. It always returns
FALSE
(i.e.,
zero) when the end of the collection has been reached. Hence, a simple,
canonical form for using an iterator is:
As an alternative, you can also use the prefix increment operator (++X). Some
iterators have other member functions for manipulating the mark, such as
findNext()
or
removeNext()
.
Member function
key()
always returns either the current object or a pointer to
the current object, again depending on whether the collection is value-based or
reference-based, respectively.
1. This is actually patterned after Stroustrup (1986, Section 7.3.2).
RWSlistCollectable list;
.
.
.
RWSlistCollectableIterator iterator(list);
RWCollectable* next;
while (next = iterator()) {
.
. // (use next)
.
}