RE:Initialization of a data memeber

From: Masaharu Goto (MXJ02154@nifty.ne.jp)
Date: Thu Aug 05 1999 - 14:21:41 MEST


Marco,

You get to the heart of the question. When you initialize data member
you can not do

  class A {
    TH1F *ptr = 0; // this is illegal C++ code
  };

Instead, you have to do

  class A {
    TH1F *ptr;
   public: 
    A() : ptr(0) { } // This syntax is important to remember
  };

Please read C++ book and find how data member and base class objects can
be initialized. It is particulary important to remember this syntax 
because in some cases, this is the only way to initizlize the member object.

Thank you
Masaharu Goto

===========================================================================
Hi all,

I encountered a small problem, of which I don't know whther it is a plain
C++ thing or zomething more CINT-specific. I think this is quite a common
thing, so I'll pose the question here:

Suppose I have a class with a Pointer data member. If I want to use this
pointer dynamically, i.e. be able to change the thing to which the pointer
points during execution, the class defention would look somthing like this:

//TTry.h:

class TTry {

public:

  TTry(TH1F& tmpH = 0);
  Fill(TH1F& tmpH); 

IsFilled();
  ~TTry() {;}

protected:
  TH1F* fHisto=0;
};


//TTry.C:
#include "TTry.h"

TTry::TTry(TH1F& tmpH)
{
   if (tmpH!=0) fHisto = new
TH1F(tmpH);
}

void TTry::Fill(TH1F& tmpH)
{
   fHisto = new
TH1F(tmpH);
}

Int_t TTry::IsFilled()
{
   if (fHisto!=0) 
     return 1;

 else
     return 0;
}

Using this class at the root-prompt:

.L TTry.C
TTry a
a.IsFilled()

Shows that the class is assumed to be filled, i.e. fHisto != 0. This can be
solved by adding a line fHisto=0 in the constructor. This behaviour seems
to be in contarst with the fact that typing TH1F *a at the ROOT-prompt
return a Null-Pointer.
The question is:
1) Is there a way to initialize class data members in the class definition?
2) Does the type TH1F* have a "default constructor", and, if so, why
doesn't it set the pointer to zero (in this case?

By the way: isn't ROOT supposed to execute the macro ~/rootlogon.C or
$HOME/rootlogon.C at startup? If it doesn't why could that be??

Thanks,

Marco van Leeuwen



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