forcing initialization of external static variables

Started by
8 comments, last by pulpfist 18 years, 5 months ago
ok here is the deal... Say I have a module a.h / a.cpp, and b.h / b.cpp b.h contains

class B
{
public:
  static long *b_handle;
  static long *get_handle() { return b_handle; }
};
b.cpp contains

static long *B::b_handle = NULL;
Now if I have an initialization routine in a.cpp that needs b_handle: a.cpp

void A::init()
{
  create_something(B::b_handle, ...         // Is b_handle possibly undefined?
  create_something(B::get_handle(), ...     // (same question)
  ...
}
I remember reading about a "hack" to make this work, something about a function returning the static variable will ensure it is initialized... Is this correct?
Advertisement
Don't quote me on this, but can static methods view other static methods?

I had thought that the static qualifier basically "detached" said functions/classes/variables from the class (as far as said function/class/var was concerned), while still allowing it to be referenced by class internals (non-static members & methods).

Am I incorrect in assuming that B::get_handle()'s body is invalid without an instance of B to refer to, being static, and thus having no concept of "this" (which is implicitely referred to by "return b_handle")?
Things change.
So long as the function isn't called by a static variable's constructor, you'll be fine. Static constructors get called all willy nilly, but once you hit main() everything gets nice and deterministic again. Until you leave main, of course, at which point you start getting static destructors getting called all willy nilly.

CM

I believe there is created and initialized 1 long* b_handle when
the b module is compiled. This variable is unique and detached, hence a common resource for the class instances.
But im not sure about these things either
Quote:Original post by Boku San
Don't quote me on this, but can static methods view other static methods?

Yes. In fact, that's all they can view.
Quote:Original post by Boku San
Am I incorrect in assuming that B::get_handle()'s body is invalid without an instance of B to refer to, being static, and thus having no concept of "this" (which is implicitely referred to by "return b_handle")?

You are correct in that static functions don't have a this pointer, however, b_handle is a static variable. So b_handle is not associated with any specific instance of the class and does not require a this pointer to access it.

CM
So what you are saying is that the use of b_handle in A::init will be undefined
outside main(), but fine inside?
@Conner: Thanks for clearing that up.

@pulpfist: As Conner mentioned, until hitting main(), static method construction is a little bit "wonky". I can't think of any instance where your static methods might be called outside of main() (though I'm sure there are at least a few).

As with before, don't quote me on this. Or do.
Things change.
Quote:Original post by pulpfist
So what you are saying is that the use of b_handle in A::init will be undefined
outside main(), but fine inside?

In a nutshell, yes. This gives a brief overview of the problem.

CM
Quote:
I can't think of any instance where your static methods might be called outside of main() (though I'm sure there are at least a few).


Whow... no wonder I start getting subtile errors working with this
new c++ stuff.
Better learn to keep shit together hehe
Thanks conner... that was the "hack" I was thinking about ^^
I think I read 1 of Scott Meyers books once

This topic is closed to new replies.

Advertisement