static members initilisation order

Just 75 points because I have no more.

I wish to know if it's possible to force the order of the statix members initialisation at runtime.
For example I have 10 classes (each one in separate files ".h" ".cpp") like :

class CTes01
{
  ...
  static LONG m_lNumber;
}
LONG CTes01::m_lNumber=1;

class CTes02
{
  ...
  static BOOL m_bFlag;
}
BOOL CTes02::bFlag=TRUE;

....

and I want to force the initialisation of CTes02::bFlag first. For the moment, the executer seems to choose the order he wants. How is possible to change this order.
VizAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
I am wrong, you can control the order of initialization of statics within a single translation unit.  The order is the order in which they are defined, like

For exanple

class A
{
static int Aint;
}

class B
{
static int BInt;
}

int A::Aint = 5; // This is first.
int B::Bint = 5; // This is second.
0
 
BinderCommented:
 Well, this is a strange request. Can you tell us why
you want to do such thing? Maybe there is another
solution to your problem.
0
 
snifongCommented:
I am not sure that I understand.
If you want to initialize m_bFlag first
declare:
class CTes02 {
public:
CTest02();
private:
BOOL m_bFlag02;
}

CTest02::CTes02():m_bFlag02(TRUE)
{
}

and declare
CTes02 a;
before you declare any instances of the other classes.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
snifongCommented:
Are these classes related?  Maybe a derived class would be a better model.
0
 
nietodCommented:
You cannot control the order of the initialization of the statics.  

You can control the order of initialization of globals that are in a single translation unit, they initialize in the order they are declared.  statics are a little trickier.  The compiler can delay their initialization until the first time they are needed.  In fact they might never be initialized.
0
 
nietodCommented:
From the C++ standard

9.4.2.7
 Static data members are initialized and destroyed  exactly  like  non-
  local objects (_basic.start.init_, _basic.start.term_).

3.6.2.1
  . . . Objects of namespace scope with static
  storage duration defined in the same translation unit and  dynamically
  initialized  shall  be initialized in the order in which their defini-
  tion  appears  in  the  translation  unit.    

Note, technically in my example, they statics might not be initalized in that order because they are POD types and beign initialized without a "real" constructor, but if they had cosntructors, the constructors would be called in that order.
0
 
VizAuthor Commented:
Nietod, you say
"you can control the order of initialization of statics within a single translation unit.  The order is the order in which they are defined"

int A::Aint = 5; // This is first.
int B::Bint = 5; // This is second.

If I was working with a stand alone executable application I would have answered "Excellent" and you would have the 75 points, ro I would have try it before asking the question, but, I am working with multiple AfxDLLs in Visual C++, and I can't put all the initialisations in the same file.

You said something concerning Global variables. Are Global variables initialised before static members ?
0
 
nietodCommented:
>> If I was working with a stand alone executable
>> application I would have answered "Excellent"
I don't have any control over the environment you are working in  Nor do I have any knowledge about it, since you didn't state any of this before.

>> I am working with
>> multiple AfxDLLs in Visual C++
They will be initialized in the order that the DLLs are loaded.  If DLL A uses DLL B, then DLL B will be loaded first and its statics and globals will be initialized before A's.

They are initialized in the same sequence, starting at the top of the translation unit.  (trivial initializations, like those for numerics, may be done before, at compile time.)
0
 
VizAuthor Commented:
Thanks nietod for the answer.
Just a last point, you didn't answer concerning global paramaters "Are Global variables initialised before static members ?"
I just logded on today on this site, and it was my first question, so did you receive the 75 points ?
0
 
nietodCommented:
>> "Are Global variables initialised before
>> static members ?"
I answered, that but didn't paste in your question, which makes it more confusing.   My answer was

>> They are initialized in the same sequence,
>> starting at the top of the translation unit.  
>> (trivial initializations, like those for numerics,
>> may be done before, at compile time.)

So they are initialized intermixed.

>> did you receive the 75 points
150.  A grade of C awards twice the question's point value.  B three times, A four times.

If an answer is not satisfactory, you can try to reach a satisfactory answer with an expert _before_ grading, especially if the problem was that you didn't supply all the necessary information.  Most questions are solved by a dialog between the expert and client, not a single response.  This gives the the client the best opportunity to obtain a complete and satifactory answer and the expert a good grade.
0
All Courses

From novice to tech pro — start learning today.