Heard that using global variables is very bad.
Hearing is not enough, you have to understand.
You should not be led to "common" opinions, even if Alexandrescu himself whispers on the phone. Solves common sense .
How many copies are broken in disputes over global variables regarding the use of the goto operator. And nothing has changed. It is not the “popularity” of designs that decides the quality of the code, but their relevance to current use.
On examples
We need a Config object. Having made it a singleton, or simply a global access structure, we do not lose anything. Overhead zero! He is alone. By making setters and getters thread-safe, we'll generally be cross-threading. But we can drive it by reference to objects ... just to let the posons say: “Bah, you don’t have global objects, you will code as the majority accepted!”
Yes, yes, of course: tens of thousands of flies can not be wrong - this is honey!
Do I need a goto ? Yes or no? "The right posony" will authoritatively say - not needed. There are primitive functional constructions with which to build everything. Only the “left” progs use this.
Let me remind you - tasks are different. For example, building an automaton on a sparse state matrix (well, for example 500x500) is much simpler on elementary switch (essentially goto ), than to build if - while nested in each other if / while / do .
Well, now subject
download any image
The essence of the "image" for the application is not a global concept. To be honest, except for the “config” (possibly, states), I no longer see applicants for the global scope.
newway, throws an exception when it is impossible to allocate memory. To check fornullptryou need to addnothrow- αλεχολυτ