It is certainly convenient, however a static
variable or a Singleton
are nothing more than global variables; and having global variables comes with drawbacks:
- the dependencies of a function become unclear: which global does it rely upon ?
- the re-entrancy of a function is compromised: what if
current_enemy.render()
accidentally callsGame::Render()
? it's an infinite recursion! - the thread-safety of a function is compromised, unless proper synchronization takes place, in which case the serialized access to the global variable bog down the performance of your concurrent code (because of Amdahl's Law)
It might seem painful and pointless to explicitly pass a reference to an instance of Game
wherever you need to, however it leaves a clear path of dependencies that can be followed and as the software grows you will appreciate explicitness.
And there is, of course, much to be said about transforming the program to have two instances of Game
. While it might seem incongruous in this precise situation, in general it is wise not to assume that it will never be necessary in the future, for we are no oracles.