static data member initialization

  • Thread starter Mike - EMAIL IGNORED
  • Start date
M

Mike - EMAIL IGNORED

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.
 
V

Victor Bazarov

Mike said:
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
 
A

Axter

Victor said:
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.
 
M

Mike - EMAIL IGNORED

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top