473,399 Members | 3,919 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,399 software developers and data experts.

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 2148
On Mar 21, 4:23 pm, "r.z." <g...@hjkjhk.plwrote:
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.plwrote:
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.plwrote:
>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.plwrote:
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.dudewrote:
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
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...
9
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...
11
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^>^...
4
by: bob | last post by:
Why doesn't c++ let me initialize static members like this: class MySound { static CSoundManager* g_pSoundManager = NULL; };
1
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...
1
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...
9
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
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...
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...
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: 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
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...
0
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...
0
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...
0
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,...
0
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...

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.