foreach, an analogy

Sean Kelly sean at f4.ca
Fri Oct 20 13:50:12 PDT 2006


Karen Lanrap wrote:
> Walter Bright wrote:
> 
>> How do you make for work with the following data structure:
>>
>>      struct Tree
>>      {     Tree* left;
>>           Tree* right;
>>           int data;
>>      }
>> ?
> 
> How do you make an iterator for foreach, if you do not own this (or 
> any other) struct or class (because it comes from a library)?

I'm not sure I understand.  foreach for a class merely calls opApply in 
that class, so the designer of the class is responsible for writing the 
iteration code.  It's simply a bit easier to write such code as a fixed 
loop or recursive function than by attempting to retain state for a C++ 
style iterator.


Sean



More information about the Digitalmars-d-announce mailing list