I have written a quite extensive framework that drives characters in a physical simulation. Even though everybody warned me not to do it, I used a global public data structure for storage of information and called it State. It’s not in a namespace either. I make it globally accessible by declaring extern State state;. The reason why I did this is because this structure is needed everywhere in the application and I find it extremely convenient to just include my State.h and then write to state.var anywhere and read state.var anywhere. The framework is changing rapidly, too, and I also find comfort in not having to care about passing data around, synchronizing etc. when new components are introduced.
Anyhow, now the s*** hit the fan. I want to use one of Qt’s GUI classes and it already has it’s own member called state of type State. Their state is at least in a namespace, but it doesn’t seem to matter, since inside the class I’m already using that namespace.
What can I do now?
I probably do not understand the problem, but what’s stopping you from doing
?
Plain
::means global namespace, and while using global symbols has the well known issues, and global variables also have their own set of issues (generally in C++ code, singletons are used instead), there’s nothing magically evil about using a global variable in the global namespace.::errnois an example of such a variable linked to practically every C and C++ application on Unix-like platforms.