Re: [ROOT] IO Problem with a TObjArray of non-TObjects

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Apr 09 2002 - 18:23:39 MEST


Hi David,

Which version are you using ? I cannot reproduce this problem.
Your program works OK for me.

Note that to store a string in a TObjArray or any other ROOT container,
you can use the class TObjString. This will be far more efficient 
than your class with multiple inheritance.

Rene Brun

David Chamont wrote:
> 
> Hi rooters,
> 
> A new version of my previous question, this time with a TObjArray.
> I would like to understand why the program below does not work.
> So to be able to collect in a TObjArray some non-TObject objects
> (TString in the example below), I use a template which transform the
> class T into a TObject via double inheritance.  When I write and reread
> the TObjArray, the program crash. Any idea ?
> 
> David C.
> 
> ===================================================
> the file with the template
> ===================================================
> 
> #include <TObject.h>
> 
> template <class T>
> class RtObj : public TObject, public T
>  {
>   public:
>     RtObj() {}
>     RtObj( const T & val ) : T(val) {}
>     ClassDefT(RtObj,1)
>  } ;
> 
> ClassDefT2(RtObj,T)
> 
> ClassImpT(RtObj,T)
> 
> ===================================================
> the linkdef file
> ===================================================
> 
> #ifdef __CINT__
> 
> #pragma link off all globals;
> #pragma link off all classes;
> #pragma link off all functions;
> 
> #pragma link C++ class RtObj<TString>+;
> 
> #endif
> 
> ===================================================
> the main file
> ===================================================
> 
> int main()
>  {
>   // begin
>   TROOT rdummy("dummy","dummy") ;
> 
>   // write
>   TFile * file =  new TFile("RtObj.root","recreate") ;
>   TObjArray array ;
>   array.AddLast( new RtObj<TString>("hello world") ) ;
>   array.Write("array",TObject::kSingleKey) ;
>   file->Close() ;
>   delete file ;
> 
>   // read
>   file =  new TFile("RtObj.root","read") ;
>   TObjArray * parray = (TObjArray *)(file->Get("array")) ;
>   cout<<*((RtObj<TString> *)((*parray)[0]))<<endl ;
>   file->Close() ;
>   delete file ;
> 
>   // end
>   return 0 ;
>  }



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:48 MET