Version 2 (modified by landauf, 8 years ago) (diff) |
---|
HowTo: ObjectLists and Iterators
Description
Every instance of a class which is in the class hierarchy is stored in a list. Additionally it's stored in the lists of all classes it is derived. So in the list of "BaseClass" you'll find instances of "BaseClass" and "DerivedClass" (if DerivedClass is derived from BaseClass as the names suggest).
Of course it's possible to iterate through all instances of a class. This is achieved by using iterators. There are two types: doc/ObjectListIterator and doc/Iterator. This page explains the difference and how to use them.
ObjectListIterator
An ObjectListIterator is used if you know at compiletime through which class you want to iterate.
for (ObjectList<MyClass>::iterator it = ObjectList<MyClass>::begin(); it != ObjectList<MyClass>::end(); ++it) { it->callSomeFunction(...); MyClass* storeTheObject = (*it); }
See doc/ObjectListIterator for detailed information.
Iterator
An Iterator is used if the class through which you want do iterate is determined at runtime. This is slower than ObjectListIterator.
Identifier* identifier = someFunctionReturningAnIdentifier(); for (Iterator<BaseObject> it = identifier->getObjects()->begin(); it != identifier->getObjects()->end(); ++it) { it->callSomeFunction(...); BaseObject* storeTheObject = (*it); }
See doc/Iterator for detailed information.