Planet
navi homeaboutscreenshotsdownloaddevelopmentforum

ObjectListIterator

TracNav(TracNav/TOC_Development)?

The ObjectListIterator of a given class T can iterate exactly through all objects of class T and inheritors. The only way to create such an iterator is by calling one of ObjectLists functions begin, end, rbegin or rend, where the ObjectList has to be of the same T as the ObjectListIterator.

Another way to iterate through object-lists is by using Iterator. ObjectListIterator however is much more performant as it knows about the right class an therefore needs no dynamic-cast. That's why you should always remember:

Important: Always use ObjectListIterator instead of Iterator if you know which class you want to iterate through.

Notation

There are two equivalent notations:

ObjectListIterator<T>

and

ObjectList<T>::iterator

Use whatever you like. Important: The second notation needs ObjectList.h, while the first notation works already with a forward declaration. So if you need the iterator in a header-file, you should prefer the first notation.

Illustration

Examples

for (ObjectList<MyClass>::iterator it = ObjectList<MyClass>::begin();
                                 it != ObjectList<MyClass>::end(); ++it)
{
    it->callSomeFunction(...);
    MyClass* storeTheObject = (*it);
}

The following example uses the class-tree below:

new A1();
new A1();
new A1();

new A3();
new A3B1();
new A3B1C1();

int count = 0;
for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin();
                                      it != ObjectList<BaseObject>::end(); ++it)
{
  count++;
}
std::cout << "BaseObject: " << count << std::endl;

count = 0;
for (ObjectList<A1>::iterator it = ObjectList<A1>::begin();
                              it != ObjectList<A1>::end(); ++it)
{
  count++;
}
std::cout << "A1:         " << count << std::endl;

count = 0;
for (ObjectList<A3B1>::iterator it = ObjectList<A3B1>::begin();
                                it != ObjectList<A3B1>::end(); ++it)
{
  count++;
}
std::cout << "A3B1:       " << count << std::endl;

/*
return:
BaseObject: 6
A1:         3
A3B1:       2
*/

Last modified 8 years ago Last modified on Oct 10, 2008, 3:27:25 AM