Re: Default constructor

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Aug 14 1998 - 09:09:46 MEST


The Default constructor is called when Root needs to create an instance
of your object. This typically happens in yourClass::Streamer in
read mode. The THtml class also invokes the default constructor.
It is a good practice to provide a default constructor that does
not allocate new objects or arrays. The default constructor
should also preset all possible data members that are pointers
to zero. The destructor must also be protected in case where
only the default constructor has been called.
Another point: Always preset your data members to some default
values (0). The memory allocator in Root (redefined new operator)
takes care of presetting all members to 0. However, in case you
do not link with the lNew library, the C++ default new is called
and it does not preset the memory.

Rene Brun

>Hi, ROOTers!

>I have one question about the role of default constructor for ROOT
>objects.

>In the class TMyClass I plan to use only one constructor:

>class TMyClass : public TObject
>{
>  public:
>    TMyClass(Int_t a);
>    void Work(void);
>  private:
>    Int_t fA;
>    ClassDef(TMyClass,1)
>};

>TMyClass::TMyClass(Int_t a)
>{
>  // fA initialising ....
>  ....
>}

>The function TMyClass::Work(void) will crash if fA is out of some legal

>range. It seems that everything is fine: TMyClass::TMyClass(Int_t) sets

>fA and TMyClass::Work(void) will work without problem. But in ROOT we
must
>have default constructor too. Will it be save to have empty default
>constructor TMyClass::TMyClass(void){} ?

>P.S. I know that it is possible to have the prototype TMyClass(Int_t
a=0)
>     but it may help only for simple cases.



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