when is okay to use global variables

Started by
13 comments, last by amemorex 21 years, 3 months ago
alright i know that global variables are really frowned on and everything but are they accepted in some cases? i mean like for example..im slowly tryin to work on an opengl engine and to be able to look around with the mouse, i have to update mouse coordinates in my WndProc whenever a WM_MESSAGE is recieved.. then elsewhere in my rendering function (totally diff .cpp file) i need to use those mouse coordinates to adjust the view and everything.. so i made the mouse coordinate variables global, makes it much easier, and everything can access them without hassle if i want them too. now is this considered okay or would all you anal-strict programmers say this is bad coding style? if so what other convoluted method would u propose?
Advertisement
Why does it matter to you if other programmers think it''s bad style?
curiousity.

global variables tend to make spaghetti code. just don''t use them too often.

Flamewars don''t make you look smart, but idiot.
I usually keep 1 or 2 global variables, however they are pointers to either structs or classes, which then store commonly used variables.

Others may not like it, but thats how I prefer to have things kept

-----------------------
"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else''s drivers, I assume it is their fault" - John Carmack
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
A function.

SetMouseCoordinates(int, int)

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]
What about the object cout? Isn''t that nothing more than just a global object?

"aut viam inveniam aut faciam" - I will either find a way or make one.

MoonStar Projects
quote:Original post by Anonymous Poster
Why does it matter to you if other programmers think it''s bad style?

Because they might have a point?



For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
global variables do have a place, but DO NOT use them in any of your C++ courses. The practice is to have the variable used within a certain scope but not the main() scope. That way things are kept tidy and are followed easier by the guy that has to update your code when you are gone....well that''s the system. =)

..but if you are creating your own projects as a hobby,do them whatever way you like..you are entitled to that freedom =P

happy codin''

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

quote: What about the object cout? Isn''t that nothing more than just a global object?


No it''s not global, it''s in the namespace std
therefore it''s bad practice making it global with "using namespace std"

This topic is closed to new replies.

Advertisement