Re: [ROOT] TObjArray::Remove

From: Edmond Offermann (edmondoffermann@yahoo.com)
Date: Mon Oct 13 2003 - 19:31:58 MEST


Hi Gero,

I am confused. The code (ok it is a few
clicks down the road) of IndexOf() shows that
it uses IsEqual(). The documentation of IsEqual()
says:

"Default equal comparison (objects are equal if
they have the same address in memory).
More complicated classes might want to override this
function."

So the default behavior is compare address in
memory ! Then YOU overwrite the default with

virtual Bool_t IsEqual(const TObject* obj) const{
  return (obj && fName ==TString(obj->GetName()));

and are surprised that it does not compare
the addresses anymore ??

Your macro just does what it is supposed to do.

Having the Remove() remove all occurences would
be a mistake too : what if I just want to remove
one of them.

If you want to get them all, just do
  while (mybla->Remove(obj))

Eddy
--- Gero Flucke <gero.flucke@desy.de> wrote:
> Hi Rooters!
> I wonder about the behaviour of
> TObjArray::Remove(TObject* obj):
> 
> 1.
> As it uses TObjArray::IndexOf(obj) it does not
> remove objects with address
> equal to obj, but the first object that answers
> kTRUE on IsEqual(obj). But
> this is what I expect from a method 'Remove' with a
> pointer as argument.
> At least the html documentation should mention that!
> A macro showing the removal of the - for my
> expectation - wrong object
> canbe found at the end.
> 
> 
> 2.
> One may also expect that all objects equal to 'obj'
> are removed from the
> array, not only  the first that matches. Here I
> admit that this is a
> matter of taste, but at least it should be
> documented in html.
> 
> Cheers
> 
> 	Gero Flucke
> 
> PS: (ROOT 3.04_02 on desy-linux4 (suse based))
> 
> PSS: Here comes the macro
> 
> 
> 
> #include "TNamed.h"
> #include "TObjArray.h"
> #include <iostream>
> 
> class MyNamed : public TNamed{
> public:
>   MyNamed(const char* name) : TNamed(name, name){}
>   virtual Bool_t IsEqual(const TObject* obj) const{
>     return (obj && fName ==
> TString(obj->GetName()));
>   }
>   virtual void Print(Option_t* opt) const{
>     this->TNamed::Print();
>     cout << this << endl;
>   }
>   ClassDef(MyNamed,1)
> };
> 
> void macro()
> {
>   MyNamed* mn1 = new MyNamed("nam1");
>   cout << mn1 << endl;
>   MyNamed* mn2 = new MyNamed("nam2");
>   cout << mn2 << endl;
>   MyNamed* mn3 = new MyNamed("nam1"); // again name
> as mn1
>   cout << mn3 << endl;
> 
>   TObjArray* arr = new TObjArray;
>   arr->Add(mn3);
>   arr->Add(mn1);
>   arr->Add(mn2);
>   arr->Print();
> 
>   cout << "\n Now removing " << mn1 << "..." <<
> endl;
> 
>   arr->Remove(mn1);
>   arr->Print();
> 
> }
> 
> giving :
> 
> root [0] .x macro.C++
> Info in <TUnixSystem::ACLiC>: creating shared
> library
>
/afs/desy.de/user/f/flucke/h1/oo/calib/GFCalib/./macro_C.so
> 0x88d4868
> 0x88d6370
> 0x88d63c0
> OBJ: MyNamed    nam1    nam1
> 0x88d63c0
> OBJ: MyNamed    nam1    nam1
> 0x88d4868
> OBJ: MyNamed    nam2    nam2
> 0x88d6370
> 
>  Now removing 0x88d4868...
> OBJ: MyNamed    nam1    nam1
> 0x88d4868
> OBJ: MyNamed    nam2    nam2
> 0x88d6370
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>
-----------------------------------------------------------------------
> Gero Flucke
> 1a / 603
> DESY
> Notkestr. 85
> 22607 Hamburg
> +49-(0)40-8998-2454



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:16 MET