Re: [TIter] don't understand a thing...

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Fri Oct 10 1997 - 15:33:39 MEST


Hi Laurent,

   what you see is a "feature". The TListIter save a pointer to
the next object before returning the current one. This so in case
you delete the current object the iteration does not get broken.
The side-effect of this is that you can not add an object to a
list when the iterator is at the last object. If you want to do
what you want don't use an iterator but do:


    Use the TObjLink list entries (that wrap the TObject*):

         TObjNum *obj;      
         TObjLink *lnk = list.FirstLink();          
         while (lnk) {                                                
            obj = (TObjNum*)lnk->GetObject();
            printf("%d  ",obj->GetNum());
            if (obj->GetNum()==3) list.Add(new TObjNum(4));
            lnk = lnk->Next();                                        
         }                                                            

Cheers, Fons.


Laurent Aphecetche wrote:
> 
> Hi,
> 
> Using the TObjNum class defined in the tcollex example ($ROOTSYS/test),
> I am trying to iter over a list, and make the list bigger inside the
> loop :
> 
>   TList list ;
>   TObjNum* obj ;
>   TIter next(&list) ;
> 
>   list.Add(new TObjNum(1)) ;
>   list.Add(new TObjNum(2)) ;
>   list.Add(new TObjNum(3)) ;
> 
>   while ( obj = (TObjNum*)next())
>   {
>     printf("%d  ",obj->GetNum()) ;
>     if (obj->GetNum()==3) list.Add(new TObjNum(4)) ;
>   }
> 
> This gives me :
> 1 2 3
> i.e, it lacks the 4.
> 
> If I change obj->GetNum()==3 by obj->GetNum()==2 (or 1), it gives me :
> 1 2 3 4
> i.e, correct.
> 
> Is this normal ?
> 
> Thanks.
> 
> --
> APHECETCHE Laurent (mailto:aphecetche@ganil.fr)
> GANIL, B.P. 5027, 14076, Caen Cedex 5, France
> Vox: +33 (0)2 31 45 45 85 - Fax: +33 (0)2 31 45 46 65
> WWW: http://ganp03.in2p3.fr/nof/ & (perso.)
> http://www.mygale.org/~p0mp0n

-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland          Phone: +41 22 7679248
E-Mail: Fons.Rademakers@cern.ch              Fax:   +41 22 7677910



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:21 MET