| 1 | = HowTo: ObjectLists and Iterators = |
| 2 | [[TracNav(TracNav/TOC_Development)]] |
| 3 | |
| 4 | == Description == |
| 5 | Every instance of a class which is in the [wiki:howto/ClassHierarchy class hierarchy] is stored in a [wiki:ObjectListBase 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). |
| 6 | |
| 7 | Of course it's possible to iterate through all instances of a class. This is achieved by using iterators. There are two types: [wiki:ObjectListIterator] and [wiki:Iterator]. This page explains the difference and how to use them. |
| 8 | |
| 9 | == !ObjectListIterator == |
| 10 | An !ObjectListIterator is used if you know at compiletime through which class you want to iterate. |
| 11 | |
| 12 | {{{ |
| 13 | for (ObjectList<MyClass>::iterator it = ObjectList<MyClass>::begin(); |
| 14 | it != ObjectList<MyClass>::end(); ++it) |
| 15 | { |
| 16 | it->callSomeFunction(...); |
| 17 | MyClass* storeTheObject = (*it); |
| 18 | } |
| 19 | }}} |
| 20 | |
| 21 | See [wiki:ObjectListIterator] for detailed information. |
| 22 | |
| 23 | == Iterator == |
| 24 | An Iterator is used if the class through which you want do iterate is determined at runtime. This is slower than !ObjectListIterator. |
| 25 | |
| 26 | {{{ |
| 27 | Identifier* identifier = someFunctionReturningAnIdentifier(); |
| 28 | |
| 29 | for (Iterator<BaseObject> it = identifier->getObjects()->begin(); |
| 30 | it != identifier->getObjects()->end(); ++it) |
| 31 | { |
| 32 | it->callSomeFunction(...); |
| 33 | BaseObject* storeTheObject = (*it); |
| 34 | } |
| 35 | }}} |
| 36 | |
| 37 | See [wiki:Iterator] for detailed information. |