Design Pattern Examples
Overview of object-oriented design patterns
Iterator< TItemType > Class Template Reference

Represents an iterator for a container by implementing the IIterator interface. More...

#include <Iterator_Class.h>

Inheritance diagram for Iterator< TItemType >:
Inheritance graph
Collaboration diagram for Iterator< TItemType >:
Collaboration graph

Public Member Functions

 Iterator (TItemType *items, size_t numItems)
 Constructor.
 
 ~Iterator ()
 Destructor.
 
bool Next (TItemType &item)
 Fetches the next item in the iteration, if any.
 
void Reset () override
 Reset the iterator to the beginning.
 
- Public Member Functions inherited from IIterator< TItemType >
virtual ~IIterator ()
 Virtual destructor so this struct can be used as an interface.
 
virtual void Reset ()=0
 Start iteration from beginning of container.
 
virtual bool Next (TItemType &item)=0
 Retrieve the next item from the container.
 

Private Attributes

TItemType * _items
 The array of items to iterate over.
 
size_t _numItems
 The number of items in the array of items.
 
size_t _index
 The index into the _items array to the next item.
 

Detailed Description

template<typename TItemType>
class DesignPatternExamples_cpp::Iterator< TItemType >

Represents an iterator for a container by implementing the IIterator interface.

This class retains a copy of the elements to be iterated over so it is immune to changes in the container from which this iterator was generated.

Template Parameters
TItemTypeThe type of each item provided by the iterator

This iterator is immune to changes in the source container. However, that makes this iterator expensive because the items being iterated over have to be duplicated in this class.

One alternative is for there to be only a single iterator and that iterator is implemented on the container class itself. If there are more than one type of iterator (as in this example) then the source container would have to implement multiple iterators, one for each type of item returned from the iterator.

Another alternative is for this iterator class to have a way to access the source container's data so the data doesn't have to be copied.

Definition at line 117 of file Iterator_Class.h.

Constructor & Destructor Documentation

◆ Iterator()

Iterator ( TItemType *  items,
size_t  numItems 
)
inline

Constructor.

Parameters
itemsThe items to iterate over.
numItemsNumber of items in the array.

Definition at line 141 of file Iterator_Class.h.

References Iterator< TItemType >::_items.

◆ ~Iterator()

~Iterator ( )
inline

Destructor.

Definition at line 153 of file Iterator_Class.h.

References Iterator< TItemType >::_items.

Member Function Documentation

◆ Next()

bool Next ( TItemType &  item)
inlinevirtual

Fetches the next item in the iteration, if any.

Parameters
itemReturns the next item.
Returns
True if an item was returned; otherwise, returns false, no more items to return

Implements IIterator< TItemType >.

Definition at line 164 of file Iterator_Class.h.

References Iterator< TItemType >::_index, Iterator< TItemType >::_items, and Iterator< TItemType >::_numItems.

◆ Reset()

void Reset ( )
inlineoverridevirtual

Reset the iterator to the beginning.

Implements IIterator< TItemType >.

Definition at line 182 of file Iterator_Class.h.

References Iterator< TItemType >::_index.

Member Data Documentation

◆ _index

size_t _index
private

The index into the _items array to the next item.

Definition at line 133 of file Iterator_Class.h.

Referenced by Iterator::Next(), Iterator< TItemType >::Next(), Iterator< TItemType >::Reset(), and Iterator::Reset().

◆ _items

TItemType* _items
private

The array of items to iterate over.

Definition at line 123 of file Iterator_Class.h.

Referenced by Iterator< TItemType >::Iterator(), Iterator::Next(), Iterator< TItemType >::Next(), and Iterator< TItemType >::~Iterator().

◆ _numItems

size_t _numItems
private

The number of items in the array of items.

Definition at line 128 of file Iterator_Class.h.

Referenced by Iterator::Next(), and Iterator< TItemType >::Next().


The documentation for this class was generated from the following file: