Hi Christian Thanks a lot for this fascinating reply, I actually ended up doing something similar to your last suggestion, i.e. put a flag in one of the objects, tested by the other object's dtor, in order to avoid a recursive nightmare: void KVTelescope::RemoveDetector(KVDetector* det, Bool_t kDeleteDetector, Bool_t kDeleteEmptyTelescope) { //Remove detector from the telescope's list and delete the detector //(if kDeleteDetector=kTRUE : default). //A check is made if the detector belongs to the telescope. //If kDeleteDetector=kFALSE detector is not deleted. //If there are no more detectors after this, delete the telescope //(unless kDeleteEmptyTelescope=kFALSE). if(fDetectors){ if( fDetectors->FindObject(det) ){ fDetectors->Remove(det); SetBit(kIsRemoving);//////set flag tested in KVDetector::~KVDetector if(kDeleteDetector) delete det; ResetBit(kIsRemoving);//unset flag tested in KVDetector::~KVDetector } //destroy this telescope if there are no detectors left in it if( fDetectors->GetSize() == 0 && kDeleteEmptyTelescope ) Delete(); } } and KVDetector::~KVDetector() { //remove from telescope's list of detectors, if in a telescope //warning: this dtor can be called by KVTelescope::RemoveDetector itself, //in this case we don't want to disappear up our own existence... if(GetTelescope()) { if(!GetTelescope()->IsRemoving()) GetTelescope()->RemoveDetector(this, kFALSE); } } which works quite nicely. Cheers John >I think this is sort of contrary to the collection design in ROOT. >Perhaps you'd be better of setting a flag in the object you may have >added to a container: > > SomeClass c; > TList l; > l.Add(&c); > c.SetInContainer(kTRUE); > > SomeClass::~SomeClass() > { > if (IsInContainer()) > ... > } > >Hmm. Perhaps some sort of smart pointer with policies usage in the >collection classes would be a good idea. I believe that in the >up-coming C++ 0x standard, the SmartPtr of [3] will be incorporated in >the Standard Library. > >Yours, > > ___ | Christian Holm Christensen > |_| | ------------------------------------------------------------- > | | Address: Sankt Hansgade 23, 1. th. Phone: (+45) 35 35 96 91 > _| DK-2200 Copenhagen N Cell: (+45) 24 61 85 91 > _| Denmark Office: (+45) 353 25 404 > ____| Email: cholm@nbi.dk Web: www.nbi.dk/~cholm > | | > >[1] http://patterndigest.com/patterns/Visitor.html >[2] http://www.hillside.net/patterns/ >[3] http://www.moderncppdesign.com/ > > > -- John D. Frankland <mailto:frankland@ganil.fr> Beam Coordinator GANIL B.P. 55027 14076 CAEN Cedex 05 *tel:* +33 (0)231454628 *fax:* +33 (0)231454665
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:16 MET