and therefore don't really understand why people hate them.
People hate global variables for three reasons:
- access to information from anywhere without formal declaration of collaboration means you can't adequately control or change how sections of code are connected.
- global variables represent application-lifetime information, in order to adequately 'reset' your program, for testing or say a 'new game' you have to manually 'drive' these values to a known good state, instead of them being destroyed naturally as part of an object graph.
- global variables are often 'autos' and the construction order of file-scope/static automatic variables is not sanely controllable
i use them is very specific ways only - ways which i'm hard pressed to find a better solution for
Look into dependecy injection.
guns don't kill people, people kill people.
Unless your gun and ammo was made by you, unsafe, and blows up at the shooting range.
i mean am i supposed to use a setter and getter for everything?
In short, yes; your public accessable interface should be well defined; reaching into an object for various internals is violating proper usage of a class.
But this is less of an issue than using globals.