Hi Thomas, One solution is to use (instead of (re)setBit) a transient data member. For example. class A : public TObject { TNamed *fNamed; bool fOwner; //! true if we own the TNamed object A() : fNamed(0),fOwner(true); ~A() { if (fOwner) delete fNamed; } void SetName(const char *name) { if (fOwner) delete fNamed; fNamed = new TNamed(name, ""); fOwner = true; } void SetName(TNamed *name) { if (fOwner) delete fNamed; fNamed = name; fOwner = false; } ClassDef(A, 1); }; The only draw back is that if the 'A' object is re-used (instead of being) re-constructed, fOwner might be incorrect. Cheers, Philippe -----Original Message----- From: owner-roottalk@pcroot.cern.ch [mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Thomas Bretz Sent: Tuesday, April 29, 2003 8:14 AM To: roottalk@pcroot.cern.ch Subject: [ROOT] Root I/O and object deletion Dear rooters, here is a simplified example to illustrate my question: If I have a class class A : public TObject { TNamed *fNamed; ~A() { if (TestBit(BIT(14)) delete fNamed; } void SetName(const char *name) { fNamed = new TNamed(name, ""); SetBit(BIT(14)); } vodi SetName(TNamed *name) { fNamed = name; ResetBit(BIT(14)); } ClassDef(A, 1) } and now I create an instance of this class A a, b; a.SetName(new TNamed); b.SetName("NewName"); and write both objects to a file: a.Write("Obj1"); b.Write("Obj2"); now I read this object into new objects: A c,d; c.Read("Obj1"); d.Read("Obj2"); As I understand it, now both objects (c and d) have a newly allocated TNamed. But for one of the two objects BIT(14) is set and for one not. How can I make sure, that the destruction of the TNamed-data-member is done correctly in any case? Thanks in advance, Thomas.
This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:11 MET