
Originally Posted by
fullmetalcoder
I'm writing a C++ component that brings some requirements and after intense web browsing I did no find any clue...
[*]How to make some pieces of code called before main() and also static constructors?[*]How to make some pieces of code called after main() and also static destructors?
I've made this trick :
// GlobalClass.h
class GlobalClass
{
static GlobalClass * m_Instance;
public :
GlobalClass();
~GlobalClass();
// here stuff, if needed...
};
// GlobalClass.h
class GlobalClass
{
static GlobalClass * m_Instance;
public :
GlobalClass();
~GlobalClass();
// here stuff, if needed...
};
To copy to clipboard, switch view to plain text mode
// GlobalClass.cpp
GlobalClass * GlobalClass::m_Instances = 0;
GlobalClass::GlobalClass ()
{
// To avoid dlouble execution...
assert ( m_Instance );
m_Instance = this;
// Before main stuff
...
}
GlobalClass::GlobalClass ()
{
m_Instance = 0;
// After main stuff
...
}
// GlobalClass.cpp
GlobalClass * GlobalClass::m_Instances = 0;
GlobalClass::GlobalClass ()
{
// To avoid dlouble execution...
assert ( m_Instance );
m_Instance = this;
// Before main stuff
...
}
GlobalClass::GlobalClass ()
{
m_Instance = 0;
// After main stuff
...
}
To copy to clipboard, switch view to plain text mode
GlobalClass GLOBALSTUFF;
int main ( int argc, char *argv[] )
{
// Here your code...
}
GlobalClass GLOBALSTUFF;
int main ( int argc, char *argv[] )
{
// Here your code...
}
To copy to clipboard, switch view to plain text mode
[*]How to ensure that this hack is portable?[/LIST]
I think that's portable...
Thanks in advance for your help.
Not at all, if I hope this was useful for you...
Bookmarks