Re: TNamedObjArray

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Tue Feb 25 1997 - 15:43:45 MET


Hi Jacek,

  because both TNamed and TObjArray inherit from TObject you get
two TObject objects in your new class (and this seems to cause
problems with CINT and the calculation of the right offsets to
the data members, Masaharu correct me here?). In principle 
TObject should be a virtual base class in such cases. However,
to make TObject a virtual base class involves many changes in 
the inheritance hierarchy. A better and simpler solution to
your problem might be to use composition (include a TNamed as
datamember while inheriting from TObjArray) or, even simpler,
to include a TString and to override just the GetName() function
to return the TString (since obj->GetName is used by 
TCollection::FindObject(char *)):

class TNamedObjArray : public TObjArray {
private:
   TString   fName;

public:
   TObjArray(char *name, ...............);
   const char *GetName() const { return fName.Data(); }
};

Cheers, Fons.


Jacek M. Holeczek wrote:
> 
> Hi,
> I just tried to create a small TNamedObjArray :
> ----------------------------------------------- TNamedObjArray.h BEGINs here
> #ifndef ROOT_TNamedObjArray
> #define ROOT_TNamedObjArray
> 
> #ifndef ROOT_TNamed
> #include "TNamed.h"
> #endif
> 
> #ifndef ROOT_TObjArray
> #include "TObjArray.h"
> #endif
> 
> class TNamedObjArray : public TNamed, public TObjArray {
> 
> public:
>    TNamedObjArray();
>    TNamedObjArray(Text_t *name, Text_t *title,
>                   Int_t s = TCollection::kInitCapacity, Int_t lowerBound = 0);
>    ~TNamedObjArray();
> 
>    ClassDef(TNamedObjArray,1) //The class title
> };
> 
> #endif
> ----------------------------------------------- TNamedObjArray.h ENDs here
> ----------------------------------------------- TNamedObjArray.C BEGINs here
> //
> // An ObjArray with a Name
> //
> 
> #include "TNamedObjArray.h"
> 
> ClassImp(TNamedObjArray)
> 
> TNamedObjArray::TNamedObjArray():TNamed(),TObjArray() {}
> 
> TNamedObjArray::TNamedObjArray(Text_t *name, Text_t *title,
>                                Int_t s, Int_t lowerBound)
>     :TNamed(name,title),TObjArray(s,lowerBound)
> {}
> 
> TNamedObjArray::~TNamedObjArray() {}
> ----------------------------------------------- TNamedObjArray.C ENDs here
> Unfortunately when compiling the dictionary ( created by rootcint )
> TNamedObjArray_dict.C I get 5 times the following error :
>     "TNamedObjArray_dict.C", line 172.17: 1540-117: (S) "TNamedObjArray"
>     has more than one base class "TObject".
> in following places :
>     twice from  "* virtual base class offset calculation interface"
>     twice from  "* Inheritance information setup/"
>     once  from  "// Read a pointer to an object of class TNamedObjArray."
> Seems I am doing something stupid here. What ?
> Thanks in advance,
> Jacek.

-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland          Phone: +41 22 7679248
E-Mail: Fons.Rademakers@cern.ch              Fax:   +41 22 7822601



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:18 MET