Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C++ Class Library Definition - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

          Classes in the ContainerIterator Hierarchy


     Each class in the class  Object-based hierarchy must have a means
     of iterating  through any sub-objects contained in the class. The
     ContainerIterator-based hierarchy fulfills this requirement.  The
     abstract  base  class of this  hierarchy,  ContainerIterator,  is
     declared  as a friend of the abstract  base  class  Object.  This
     relationship means that  you  can apply iterators to every object
     in  the  class  Object  hierarchy  without  having  to  know  the
     derivation of the object which you are iterating.

     Iterators  occur  frequently  in  programs  written   in   C.  To
     illustrate  this  situation,  we  give  an example of the use  of
     iterators in C and translate that example to C++. The following C
     code fragment uses an iterator to step through the elements of an
     array of strings.

        char *text[] = { "this", "is", "some", "text", 0 };
        char **cur = text;

        while ( *cur != 0 )
        {
           . . .
           cur++;
        }

     The steps of the iterations are:

        1.  Initialize the iterator. This  occurs  in  the declaration
            and initialization statement which begins 'cur *text[] = {
            . . . .'

        2.  Test the iterator for completion of the iterating process.
            This occurs in the  conditional  of  the  while statement,
            'while ( *cur != 0 ).'

        3.  Increment the iterator.  This  is  done  by  the statement
            'cur++.'

     We  extend this form in C++. The above  example  can  be  written
     using an instantiation of an iterator class as follows.

        Array myArray( . . . );
        ContainerIterator myIterator = myArray.initIterator();
        while ( myIterator != 0 )
        {
           . . .
           myIterator++;
        }

     To be an iterator, therefore, a class must do the following:

        1.  Overload   the   preincrement   operator,  operator  ++().
            Operator ++() should return a reference to the next object
            in the iteration sequence.

        2.  Provide an operator which  converts  the  iterator  to the
            predefined type int.    This  conversion  is  necessary to
            inform  users  of  the  iterator  that there are  no  more
            objects left in the iteration sequence.

        3.  Provide a function to restart the iteration process at any
            time after a program creates the iterator.

     The iterators available in  the  class  library are listed below,
     along  with  their  member functions.   You  may  note  that  the
     DoubleListIterator  class  defines  the  predecrement   operator,
     operator --(), in addition to the other required functions.  This
     operator appears in class DoubleListIterator since it is possible
     to traverse a DoubleList in either direction.

     Class ContainerIterator:

     Public member functions:
        operator ++
        operator int
        restart


     Class ArrayIterator, derived from ContainerIterator

     Public member functions:
        operator ++
        operator int
        restart


     Class DoubleListIterator, derived from ContainerIterator

     Public member functions:
        operator ++
        operator --
        operator int
        restart


     Class ListIterator, derived from ContainerIterator

     Public member functions:
        operator ++
        operator int
        restart

See Also: array.h dbllist.h list.h

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson