473,395 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2170
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
19
by: JustSomeGuy | last post by:
I have a class that has a static member variable. string x; x should never change during use and should be intialized to "abcd". How does one do this?
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.