Hi,
Since static members do not belong to a particular class object, they cannot
be initialized inside a constructor. You should initialize them outside
class TestClass {
public:
static const std::string name;
};
...
const std::string TestClass::name = "default-name";
You can also initialize integer and enum - like static constants inside the
class declaration (although this does not always work with root).
class TestClass {
public:
static const int value = 10;
enum {anotherValue = 20};
};
Cheers,
Andriy.
-----Original Message-----
From: owner-roottalk@pcroot.cern.ch
[mailto:owner-roottalk@pcroot.cern.ch]On Behalf Of Conrad Steenberg
Sent: Friday, August 30, 2002 6:39 PM
To: Jiri Masik
Cc: ROOT mailing list; Mykulyak Andriy
Subject: Re: [ROOT] static consts as class members
Hi
On a related subject, what are the rules for initializing const members
outside the class definitions? My C++ books don't cover that very well
;-)
E.g.
class myclass : public TObject {
public:
const Int_t count;
...
}
The following doesn't compile:
myclass::myclass(Int_t newcount){
count=newcount;
}
I thought you got once chance to initialize a const, and that the
constructor may be the place to do it, but this is obviously not the
case :-)
Cheers!
Conrad
On Fri, 2002-08-30 at 06:40, Jiri Masik wrote:
> Mykulyak Andriy <A.Mykulyak@gsi.de> writes:
>
> > Hello,
> >
> > I wonder, why I cannot use consts as static class members.
> >
> > class TAFextPropHit : public TObject {
> > public:
> > static const Int_t kSignalCount = 8;
> > static const Float_t kScaleFactor = 1.5;
> >
> > TAFextPropHit();
> > virtual ~TAFextPropHit();
> > ...
> >
> > ClassDef(TAFextPropHit, 1)
> > ...
> > };
> >
> > The class above will compile, but the shareable library built out of it
will
> > not load into the ROOT runtime.
> >
>
> Hi,
>
> I think the initialization of a static data member should happen
> outside of the class definition
>
> class TAFextPropHit {
> public:
> static const Int_t kSignalCount;
> };
> const Int_t TAFextPropHit::kSignalCount=8;
>
> What kind of error do you get?
> cheers
> Jiri
>
--
*-----------------------------------------*
| Conrad Steenberg |
| Caltech, Mail Code 356-48 |
| Pasadena, CA, 91125 |
| e-mail: conrad@hep.caltech.edu |
| Tel: (626) 395-8758 |
*-----------------------------------------*
This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:06 MET