Iterators : The proper way

यदा यदा हि धर्मस्य ग्लानिर्भवति भारत

धर्मसंस्थापनार्थाय सम्भवामि युगे युगे ॥7-8, Chapter 4॥

A shalok or verse, from Geeta, that is known to many Hindus, or the gist of which gets reflected in our actions (I am a Hindu) anyway, is the representation of iterative theme, of course, coupled with a right action mentioned above. The meaning implies that Shri Krishna takes birth in an epoch, time again, whenever there is rise of evil, to establish law and order.

Unreal Engine has a way of iterating over the C++ objects which has been used in hundreds of AAA games covering a stretch of couple of decades.

The iteration is done like so

for (TActorIterator<AActor> ActorItr(testWorld); ActorItr; ++ActorItr)
{
	// print name
	KR_INFO("Iterating over actor: {0}", (*ActorItr)->GetName());
}

The TActorIterator template is defined here. The pseudo code for iteration roughly is

  1. Initialize ActorList with the appropriate UObjects (of a world or editor relevant objects for instance) of cached objects.
  2. Appropriately define (overload) the increment (++) operator with desired class filters and appropriate checks

A fruitful thing is to think what remains constant and what doesn’t in this way of iteration, which in turn defines the concept of iteration.

Participate in discussion