Thanks for the answer Andy!
I'm not sure if you will read this but I'm going to ask a question anyway...
Regarding the two phase construction I prefer to stick to a singleton pattern here.
Meaning I removed all public means of constructing the object and instead require the usage of a static create method.
I even removed all means of copying the object since its contents contains various references to system resources that simply aren't trivial to just share with another object.
Now I'm essentially trying to evaluate two methods of storing globals.
The one you describe using a single application/module object.
The other is to use a namespace with POD (Plain Old Data).
my_globals.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
// misc include directives here
namespace my_globals
{
typedef enum
{
NotInitialized,
InitializationSucceeded,
InitializationFailed,
} InitializationState;
external InitializationState api_init;
void init();
void finish();
} // namepace
|
my_globals.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
// misc include directives here
namespace my_globals
{
InitializationState api_init = NotInitialized;
void init()
{
if ( api_init == NotInitialized )
{
if ( initialize_external_api() != ANY_ERROR )
api_init = InitializationSucceeded;
else
api_init = InitializationFailed;
}
}
void finish()
{
if ( api_init == InitializationSucceeded )
{
finish_external_api();
api_init = NotInitialized;
}
}
} // namespace
|
I guess that either of the two methods works but since this is one of the first times I seriously think about this issue with globals I want to try to do it right from start instead of banging my head into some unnecessary complication at a much later stage where the code will be harder to "just fix".
I read a little from Google's C++ Style Guide and they specifically mention that using objects have potential pitfalls:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Static_and_Global_Variables
Short version: Object order creation/deletion as an application starts up / shuts down are only partially specified in the C++ specs and can vary between different builds.
Anyway I have a question to you, or anyone else for that matter.
Do you know if there are possible advantages over the object method as compared to using a namespace with POD?