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

From: David Chamont (David.Chamont@poly.in2p3.fr)
Date: Tue Apr 09 2002 - 17:26:30 MEST


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