Original Post
Hello all, I'm having a problem with the order of static constructors not being called in the order I desire on startup and wrecking all sorts of havoc. Since my code is quite a bit complicated, I'll try to simplify. Given a simple "Texture" class, for example -- it wants to register itself to some kind of a manager in its constructor: Until now, all of my MyTexture objects have been created inside or after main() was called, so there haven't been any problems. I recently tried creating a static MyTexture outside of main() and have found that the textureManager vector hasn't been initialized yet (_Buy(0) hasn't been called so the internal _Myfirst, _Mylast, _Myend variables are just garbage). Just to make sure, I put a breakpoint inside the vector() constructor and my classes' constructor, and my class got initialized first. Surely there must be quite a few people out there who have done something like this before. I did a fairly exhaustive forum search, but nothing seemed to turn up. The only way I can see how to fix this is to declare textureManager as a pointer, and dynamically create it if it hasn't been allocated before. Something like this, perhaps: But this just seems really yucky. Plus, I can't use the C++ operators directly, so I'd have to do:
static std::vector<MyTexture *> textureManager;
MyTexture::MyTexture()
{
textureManager.push_back(this);
// ...do stuff here...
}
static std::vector<MyTexture *> *textureManager = NULL;
MyTexture::MyTexture()
{
if (textureManager == NULL)
textureManager = new std::vector<MyTexture *>;
textureManager->push_back(this);
// ...do stuff here...
}
MyTexture *tex = (*textureManager)[idx]; Is there anyone who's dealt with this before? What's the best way to handle a situation like this? Any thoughts / opinions are welcome. [Edited by - bpoint on May 28, 2006 9:47:07 AM]