Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of code/howto/Iterator


Ignore:
Timestamp:
Oct 10, 2008, 3:29:43 AM (16 years ago)
Author:
landauf
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/howto/Iterator

    v1 v1  
     1= HowTo: ObjectLists and Iterators =
     2[[TracNav(TracNav/TOC_Development)]]
     3
     4== Description ==
     5Every 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
     7Of 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 ==
     10An !ObjectListIterator is used if you know at compiletime through which class you want to iterate.
     11
     12{{{
     13for (ObjectList<MyClass>::iterator it = ObjectList<MyClass>::begin();
     14                                 it != ObjectList<MyClass>::end(); ++it)
     15{
     16    it->callSomeFunction(...);
     17    MyClass* storeTheObject = (*it);
     18}
     19}}}
     20
     21See [wiki:ObjectListIterator] for detailed information.
     22
     23== Iterator ==
     24An Iterator is used if the class through which you want do iterate is determined at runtime. This is slower than !ObjectListIterator.
     25
     26{{{
     27Identifier* identifier = someFunctionReturningAnIdentifier();
     28
     29for (Iterator<BaseObject> it = identifier->getObjects()->begin();
     30                          it != identifier->getObjects()->end(); ++it)
     31{
     32    it->callSomeFunction(...);
     33    BaseObject* storeTheObject = (*it);
     34}
     35}}}
     36
     37See [wiki:Iterator] for detailed information.