Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

The NullIterator Class

 

A NullIterator is a concrete iterator that does nothing. As discussed in Section gif, every container has a NewIterator member function that returns a reference to an iterator instance. If a particular concrete container class does not provide an associated iterator class, its NewIterator member function returns a reference to a NullIterator. Program gif gives the declaration of the NullIterator class.

   program5101
Program: NullIterator Class Definition

Because it is a concrete class, NullIterator provides implementations for all the pure virtual member functions of the Iterator class. As shown in Program gif, the implementation is trivial: The IsDone member function always returns true; the operator* member function always returns a reference to the NullObject instance; and the remaining functions do nothing at all.

   program5125
Program: NullIterator Class Member Function Definitions

The default behavior of the NewIterator function of the Container class is to return a reference to a a new NullIterator instance as shown in Program gif.

   program5145
Program: Container Class NewIterator Member Function Definition


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.