Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

amemorex

when is okay to use global variables

This topic is 5655 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Why does it matter to you if other programmers think it''s bad style?

Share this post


Link to post
Share on other sites
global variables tend to make spaghetti code. just don''t use them too often.


Flamewars don''t make you look smart, but idiot.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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''

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!