473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::myMeth od()
{
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 6380
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::myMeth od()
{
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::myMeth od()
{
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(){sta tic 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::myMeth od()
> {
> 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(){sta tic 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
1806
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
3600
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 non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a DLL by another external object file (not within the DLL). It only occurs when the static const...
7
2474
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
8
8925
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. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
4
8359
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 should initialize them in advance. However, the following code, in which I first produce two classses and then try to assign a reference of the first class to a static data member of the second class doesn't work. It gives the following compiler error:
5
5614
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
2
1831
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Base { public: Base(int x = 0);
15
7859
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
2480
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
9160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8862
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6521
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.