469,934 Members | 2,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Initialization of static members/constants???

I just stumbled across the following problem:

//.h
class Masses
{
static double mass1;
static double mass2;
static double mass3;
};

//.cpp
double Masses::mass1 = 123.0;
double Masses::mass2 = 456.0;
double Masses::mass3 = mass1/mass2;
// elsewhere (other translation unit)
double m = Masses::mass3; // m and Masses::mass3 == 0!
If I change mass3 initialization to a non-arithmetic literal, all works; leave
as arithmetic operation, and the value is set to 0.

So, my question is this: does the language mandate the initialization order of
such constants? I thought that it was based on the declaration order in the
class definition, but if that is the case, then mass3 should have a non-zero
value.

Is this a problem w/ my compiler (MSVC 2003), or a misunderstanding of the
language on my part?

FWIW: I also tried using static const in a namespace instead of as class
members, same behavior.

Thanks
Jul 22 '05 #1
4 2006
Bret Pehrson wrote:
I just stumbled across the following problem:

//.h
class Masses
{
static double mass1;
static double mass2;
static double mass3;
};

//.cpp
double Masses::mass1 = 123.0;
double Masses::mass2 = 456.0;
double Masses::mass3 = mass1/mass2;
// elsewhere (other translation unit)
double m = Masses::mass3; // m and Masses::mass3 == 0!
If I change mass3 initialization to a non-arithmetic literal, all works; leave
as arithmetic operation, and the value is set to 0.

So, my question is this: does the language mandate the initialization order of
such constants?
Only within the same translation unit.
I thought that it was based on the declaration order in the
class definition, but if that is the case, then mass3 should have a non-zero
value.

Is this a problem w/ my compiler (MSVC 2003), or a misunderstanding of the
language on my part?
Yes, it's a misunderstanding. Read about "static initialisation order
fiasco" in the FAQ ( http://www.parashift.com/c++-faq-lite/ )
FWIW: I also tried using static const in a namespace instead of as class
members, same behavior.


Placing them in a namespace instead of a class shouldn't really matter.

V
Jul 22 '05 #2
Victor Bazarov wrote:

Bret Pehrson wrote:
I just stumbled across the following problem:

//.h
class Masses
{
static double mass1;
static double mass2;
static double mass3;
};

//.cpp
double Masses::mass1 = 123.0;
double Masses::mass2 = 456.0;
double Masses::mass3 = mass1/mass2;
// elsewhere (other translation unit)
double m = Masses::mass3; // m and Masses::mass3 == 0!
If I change mass3 initialization to a non-arithmetic literal, all works; leave
as arithmetic operation, and the value is set to 0.

So, my question is this: does the language mandate the initialization order of
such constants?


Only within the same translation unit.
> I thought that it was based on the declaration order in the
class definition, but if that is the case, then mass3 should have a non-zero
value.

Is this a problem w/ my compiler (MSVC 2003), or a misunderstanding of the
language on my part?


Yes, it's a misunderstanding. Read about "static initialisation order
fiasco" in the FAQ ( http://www.parashift.com/c++-faq-lite/ )


Yes, but these constants are declared/initialized in the same translation
unit. In my sample above, the .h represents 1 header, .cpp 1 source, and
elsewhere, is just the use of those class statics in some other context other
than the .cpp translation unit.

According to my understanding and what I've read, the SIOF only applies to
static initialization _across_ translation units, not within.

I'm presuming this is a compiler bug, and have simply resorted to using
non-arithmetic literals for constants initializers.
Jul 22 '05 #3
Bret Pehrson wrote:
Victor Bazarov wrote:
Bret Pehrson wrote:
I just stumbled across the following problem:

//.h
class Masses
{
static double mass1;
static double mass2;
static double mass3;
};

//.cpp
double Masses::mass1 = 123.0;
double Masses::mass2 = 456.0;
double Masses::mass3 = mass1/mass2;
// elsewhere (other translation unit) ^^^^^^^^^^^^^^^^^^^^^^double m = Masses::mass3; // m and Masses::mass3 == 0!
[...]

Yes, but these constants are declared/initialized in the same translation ^^^^^^^^^^^^^^^
Uh, who is crazy here, I or you?
unit. In my sample above, the .h represents 1 header, .cpp 1 source, and
elsewhere, is just the use of those class statics in some other context other
than the .cpp translation unit.

According to my understanding and what I've read, the SIOF only applies to
static initialization _across_ translation units, not within.
So, if the 'm' object is in a different ("other") translation unit than
the 'Masses::mass3' object, would it make them "within" or "across"?
I'm presuming this is a compiler bug, and have simply resorted to using
non-arithmetic literals for constants initializers.


No, it's not a compiler bug.

V
Jul 22 '05 #4
Victor Bazarov wrote:

Bret Pehrson wrote:
Victor Bazarov wrote:
Bret Pehrson wrote:

I just stumbled across the following problem:

//.h
class Masses
{
static double mass1;
static double mass2;
static double mass3;
};

//.cpp
double Masses::mass1 = 123.0;
double Masses::mass2 = 456.0;
double Masses::mass3 = mass1/mass2;
// elsewhere (other translation unit) ^^^^^^^^^^^^^^^^^^^^^^double m = Masses::mass3; // m and Masses::mass3 == 0!
[...]

Yes, but these constants are declared/initialized in the same translation

^^^^^^^^^^^^^^^
Uh, who is crazy here, I or you?


Well, not me, but I'll admit to a poorly constructed sample -- see below.
unit. In my sample above, the .h represents 1 header, .cpp 1 source, and
elsewhere, is just the use of those class statics in some other context other
than the .cpp translation unit.

According to my understanding and what I've read, the SIOF only applies to
static initialization _across_ translation units, not within.


So, if the 'm' object is in a different ("other") translation unit than
the 'Masses::mass3' object, would it make them "within" or "across"?


Ok, I see the point of confusion. m isn't another static, simply a local
variable. A more accurate complete snippet should be:

// assuming previous .h and .cpp snippet from op

// elsewhere (other translation unit)
int main()
{
double m = Masses::mass3; // m and Masses::mass3 == 0!
return 0;
}

This is what I intended to portray in my original sample, but didn't adequately
describe that.

I'm presuming this is a compiler bug, and have simply resorted to using
non-arithmetic literals for constants initializers.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by JustSomeGuy | last post: by
8 posts views Thread by Per Bull Holmen | last post: by
20 posts views Thread by JohnQ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.