Sign in to follow this  
pulpfist

forcing initialization of external static variables

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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")?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
@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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this