Represents an iterator for a container by implementing the IIterator interface. More...
#include <Iterator_Class.h>
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. | |
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.
TItemType | The 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.
|
inline |
Constructor.
items | The items to iterate over. |
numItems | Number of items in the array. |
Definition at line 141 of file Iterator_Class.h.
References Iterator< TItemType >::_items.
|
inline |
Destructor.
Definition at line 153 of file Iterator_Class.h.
References Iterator< TItemType >::_items.
|
inlinevirtual |
Fetches the next item in the iteration, if any.
item | Returns the next item. |
Implements IIterator< TItemType >.
Definition at line 164 of file Iterator_Class.h.
References Iterator< TItemType >::_index, Iterator< TItemType >::_items, and Iterator< TItemType >::_numItems.
|
inlineoverridevirtual |
Reset the iterator to the beginning.
Implements IIterator< TItemType >.
Definition at line 182 of file Iterator_Class.h.
References Iterator< TItemType >::_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().
|
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().
|
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().