473,569 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

where/when should I initialize static class data members?

They should be initialized before any instance is created. I have no idea in
which file, in which place should I put their initialization code to be sure
they are initialize only once, before any instance is created.
I cannot visualize the control flow of an executing program when the code is
split in multiple files.


Mar 21 '07 #1
6 2157
On Mar 21, 4:23 pm, "r.z." <g...@hjkjhk.pl wrote:
They should be initialized before any instance is created. I have no idea in
which file, in which place should I put their initialization code to be sure
they are initialize only once, before any instance is created.
I cannot visualize the control flow of an executing program when the code is
split in multiple files.
Except for integral constants, which may be initialized in the class
definition, you generally want them in the .cpp file associated with
your .hpp file. If the code for a class needs to be split into
multiple .cpp files, that may be an indication that your class is too
big and is doing to much (time to refactor!). The code to initialize
static vars gets run automatically and only once before main()
executes. Often order of initialization doesn't matter, but if it
does, there are some subtle problems that can crop up. See this FAQ
and those following:

http://www.parashift.com/c++-faq-lit...html#faq-10.12

Cheers! --M

Mar 21 '07 #2
r.z. <gk***@hjkjhk.p lwrote:
They should be initialized before any instance is created. I have no idea in
which file, in which place should I put their initialization code to be sure
they are initialize only once, before any instance is created.
I cannot visualize the control flow of an executing program when the code is
split in multiple files.
This sounds like it might be a case of the "static initialization order
fiasco". Maybe this entry (and the ones following it) in the FAQ can
provide some insight:

http://www.parashift.com/c++-faq-lit...html#faq-10.12

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 21 '07 #3
Marcus Kwok wrote:
r.z. <gk***@hjkjhk.p lwrote:
>They should be initialized before any instance is created. I have no idea in
which file, in which place should I put their initialization code to be sure
they are initialize only once, before any instance is created.
I cannot visualize the control flow of an executing program when the code is
split in multiple files.

This sounds like it might be a case of the "static initialization order
fiasco". Maybe this entry (and the ones following it) in the FAQ can
provide some insight:

http://www.parashift.com/c++-faq-lit...html#faq-10.12
Now, I'm concerned on my own code. Will I hit the initialization order
fiasco on this code?

// A.h

class B;
class A {
private:
static B x;
public:
static void f();
};

// A.cpp

#include "A.h"
#include "B.h" // details irrelevant, assume
// void B::do_something () exists

B A::x;

void A::f()
{
x.do_something( );
// do other stuff
}

// main.cpp

#include "A.h"

int main()
{
A::f();
}
Mar 21 '07 #4
On Mar 21, 9:23 pm, "r.z." <g...@hjkjhk.pl wrote:
They should be initialized before any instance is created. I have no idea in
which file, in which place should I put their initialization code to be sure
they are initialize only once, before any instance is created.
I cannot visualize the control flow of an executing program when the code is
split in multiple files.
One of possibilities is if the references to static are local to
single function/single cpp file, the var should be defined in that
file.

Mar 21 '07 #5
On 21 Mar, 21:05, red floyd <no.s...@here.d udewrote:
Now, I'm concerned on my own code. Will I hit the initialization order
fiasco on this code?
No. The static initialisation order fiasco is an issue when you have
*two* different static objects (e.g. obj1 and obj2) defined in
*different* source files *and* your code relies on one being
constructed before the other (e.g. during construction of obj1, obj1
calls a member function obj2). The problem is that there is no way you
can control which of obj1 and obj2 is constructed first, but if obj1
happens to be constructed first there is no obj2 yet for it to use.
Bang - you're dead.

Your code below only contains one static object, A::x, of type B.
// A.h

class B;
class A {
private:
static B x;
public:
static void f();

};

// A.cpp

#include "A.h"
#include "B.h" // details irrelevant, assume
// void B::do_something () exists

B A::x;

void A::f()
{
x.do_something( );
// do other stuff

}

// main.cpp

#include "A.h"

int main()
{
It is guaranteed that, by the time you enter main here, A::x is fully
constructed, therefore the call to A::f() below, which in turn calls a
member function of A::x, is fine.
A::f();
}
What would be a problem would be if you had something like this (not
tested):

// C.h
#include "A.h"
class C
{
public:
// Constructor indirectly calls do_something() on A::x
C() { A::f(); }
};

and you changed main.cpp to

#include "A.h"
#include "C.h"

C c;

int main()
{
A::f();
}

Now A::x and c are defined in different translation units and you have
no control over which is constructed first. But the construction of c
involves calling do_something() on A::x. So if the compiler happens to
choose to construct c first, you suffer death by fiasco.

Gavin Deane

Mar 22 '07 #6
Gavin Deane wrote:
[incredibly lucid explanation redacted]
Many thanks, Gavin!

Mar 22 '07 #7

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

Similar topics

2
550
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const char* ToString() { return "Object"; } virtual DataType GetType() { return DataType( "Object" ); } protected: const static DataType _dataType( "Object");
9
2658
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value type-stored on the stack Regards thomson
11
2250
by: Geoff Cox | last post by:
Hello, I am trying to get a grip on where to place the initialization of two arrays in the code below which was created using Visual C++ 2005 Express Beta 2... private: static array<String^>^ LHSquestions = gcnew array<String^> {"question 1","question 2"}; private: static array<String^>^ RHSquestions = gcnew array<String^> {"question 1",
4
2546
by: bob | last post by:
Why doesn't c++ let me initialize static members like this: class MySound { static CSoundManager* g_pSoundManager = NULL; };
1
1954
by: Alan Johnson | last post by:
Consider the following class template, which has the static member 'seed_'. This template is intended to be inherited by several other classes using the curiously recurring template pattern, so that each class gets its own copy of 'seed_'. I am not sure where to initialize the static member, though. Putting it in the header as indicated...
1
1676
by: pasa_1 | last post by:
Can someone clarify few items from FAQ http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 ' Should my constructors use "initialization lists" or "assignment"?' a. This might happen when your class has two constructors that need to initialize the 'this' object's data members in different orders. <-- Okay
9
27377
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static Date default_date; };
24
5325
by: Paul | last post by:
I am taking over an existing app and having trouble understanding their references. They have encapsulated Pear packages in the directory structure like: /site /site/html/Pear.php /site/html/Sigma.php /site/html/Common.php /site/html/Quickform.php /site/html/Quickform/
4
8349
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...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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...
0
6286
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...
1
5514
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...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
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...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
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
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.