473,387 Members | 1,497 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,387 software developers and data experts.

static data member initialization

MyClass
{
//I have a static member method:
void static myMethod();
//and a static data member:
static MyType myData;
};

//In the .cpp file:
void MyClass::myMethod()
{
if (myData == myInitialValue)
...
}
//and below, in the same file:
MyType MyClass::myData = myInitialValue;

Can I rely on initialization in time for the if
statement? Chapter and verse?

Thanks for your help.
Mike.

Feb 9 '06 #1
3 6346
Mike - EMAIL IGNORED wrote:
MyClass
{
//I have a static member method:
void static myMethod();
//and a static data member:
static MyType myData;
};

//In the .cpp file:
void MyClass::myMethod()
{
if (myData == myInitialValue)
...
}
//and below, in the same file:
MyType MyClass::myData = myInitialValue;

Can I rely on initialization in time for the if
statement? Chapter and verse?


Objects with static storage duration (static data members included)
defined outside of any function scope are initialised before 'main'
is called. So, if youre 'myMethod' function is called in the course
of normal program execution, 'myData' will have been initialised.
If the 'myMethod' function is called during another static object
initialisation, all bets are off, unless that object is declared
_after_ 'myData' in the _same_ translation unit.

V
--
Please remove capital As from my address when replying by mail
Feb 9 '06 #2
Victor Bazarov wrote:
Mike - EMAIL IGNORED wrote:
MyClass
{
//I have a static member method:
void static myMethod();
//and a static data member:
static MyType myData;
};

//In the .cpp file:
void MyClass::myMethod()
{
if (myData == myInitialValue)
...
}
//and below, in the same file:
MyType MyClass::myData = myInitialValue;

Can I rely on initialization in time for the if
statement? Chapter and verse?


Objects with static storage duration (static data members included)
defined outside of any function scope are initialised before 'main'
is called. So, if youre 'myMethod' function is called in the course
of normal program execution, 'myData' will have been initialised.
If the 'myMethod' function is called during another static object
initialisation, all bets are off, unless that object is declared
_after_ 'myData' in the _same_ translation unit.


Actually, the complete answer is a little more complicated than that.
If MyType is a POD type, then it's garanteed to be initialized before
any global complex (non-POD) type. This includes static data members.
Even if not in same translation unit.
You can not ganrantee the order of initialization if MyType is a
non-POD type, and it's access from a global object's constructor that
is in a different translation unit.

An easy work around method for this is to create the object as a static
local variable within a static function.

class MyClass
{
void static myMethod();
//Replace above with following:
inline static MyType& GetMyData(){static MyType myData;}
};

Now your variable is garanteed to be initialized IAW C++ standard when
you call it.
However, if it's called from a global object's destructor, you will
still have issues with order of destructor.

Feb 9 '06 #3
On Thu, 09 Feb 2006 10:06:19 -0800, Axter wrote:
Victor Bazarov wrote:
Mike - EMAIL IGNORED wrote:
> MyClass
> {
> //I have a static member method:
> void static myMethod();
> //and a static data member:
> static MyType myData;
> };
>
> //In the .cpp file:
> void MyClass::myMethod()
> {
> if (myData == myInitialValue)
> ...
> }
> //and below, in the same file:
> MyType MyClass::myData = myInitialValue;
>
> Can I rely on initialization in time for the if
> statement? Chapter and verse?


Objects with static storage duration (static data members included)
defined outside of any function scope are initialised before 'main'
is called. So, if youre 'myMethod' function is called in the course
of normal program execution, 'myData' will have been initialised.
If the 'myMethod' function is called during another static object
initialisation, all bets are off, unless that object is declared
_after_ 'myData' in the _same_ translation unit.


Actually, the complete answer is a little more complicated than that.
If MyType is a POD type, then it's garanteed to be initialized before
any global complex (non-POD) type. This includes static data members.
Even if not in same translation unit.
You can not ganrantee the order of initialization if MyType is a
non-POD type, and it's access from a global object's constructor that
is in a different translation unit.

An easy work around method for this is to create the object as a static
local variable within a static function.

class MyClass
{
void static myMethod();
//Replace above with following:
inline static MyType& GetMyData(){static MyType myData;}
};

Now your variable is garanteed to be initialized IAW C++ standard when
you call it.
However, if it's called from a global object's destructor, you will
still have issues with order of destructor.


Interesting. I was using the static method you mention, but I
had several such variables, and I wondered of it was really
necessary. Since they are all POD, I see that it is not.

By the way, when you say POD, can I take this literally,
thereby including POD structs?

Also, can anyone point to the sections in the standard that
one would read to draw these conclusions?

Thanks for your comments.

Mike.

Feb 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
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...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
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....
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
5
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
2
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
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?
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.