Archived

This topic is now archived and is closed to further replies.

kamrann

Initialization order for global/static variables

Recommended Posts

I''ve just discovered a fairly annoying problem in my program. I have a static library for general 3D functions, which includes a Vector class. The Vector class includes a couple of const static members of the Vector type itself, declared in a .cpp file: const Vector Vector::ORIGIN = Vector(0, 0, 0); const Vector Vector::Z = Vector(0, 0, 1.0f); ... etc. Now, in my program which is linked to this library, in my main cpp file, at the top, I declare a global variable of a class A, where A is defined elsewhere as class A { public: Vector m_Facing; A(); ... }; and in its cpp file: A::A() { m_Facing = Vector::Z; } problem is, this object of type A is getting constructed before the Vector class''s static const members are initialized, and so m_Facing is given the value (0, 0, 0). I''ve realized there are lots of other places where this could happen, for instance trying to initialze some static const class members using static const members of another class, but the initialization order makes it go wrong. So basically, is there any way to tell MSVC++ the order in which I want things initialized at startup, or any other way to get round this problem? thanks Cameron

Share this post


Link to post
Share on other sites
thats fine - i dont intend to keep the globals, they''re just there because I wanted to make a quick win32 app to try some things out. However, as I said, this has also caused problems when trying to make use of static const members of one class to initialize static const members of another. Right now I can''t actually think where I might have needed to do this, but i''m sure I did in at least one place. Is there some way to set the order of initialization of my classes, or will I just have to remove these dependencies?

thanks

Cameron

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There is an interesting technique how to deal with this problem which I found in book "Thinking in C++". The idea is that whenever you have global variable or static member which depends on another global variable or static member, you place that variable inside a function that returns a reference to that object. For example

class A
{
public:
A(B& b); // class A depends on class B
}

extern B b; // global variable defined in other file
A a(b); // global variable

is dangerous because b can be created after a. But if you put b inside some funcion

B& b_func()
{
static B b;
return b
}

and then define a like this

A a(b_func());

it is guaranteed that b will be created before a (because static variables in functions are created when this function is called first time).

Share this post


Link to post
Share on other sites
thanks, i''ll give it a try.
still, it seems a really messy thing to do. I realized that removing globals doesn''t really have any effect. You need at least one global, for your application class. Then if you have your objects embedded inside the app class, then they are going to be constructed when the app object is constructed, and since the app is global, this is at the same time as if the objects had been declared global themselves.

What I really can''t understand, is why CONST static members aren''t all initialized before non-const globals - this just seems stupid; surely all consts should be initialized first, since their initialization can''t depend on any non constants anyway.

any idea anyone?

Cameron

Share this post


Link to post
Share on other sites
Instantiate an empty object of type A, and initialize it later on in the program, like in an intialization function:

  
A a; // uses default constructo to create an empty object


void Initialize(void)
{
a.SetVector(Vector::Z);
// other stuff

}

A::A()
{
m_facing = {0, 0, 0}
}

A::SetVector(Vector vec)
{
m_facing = vec;
}


Paradigm Shift 2000

Share this post


Link to post
Share on other sites