Global Variable Concept Questions...

Started by
5 comments, last by Chryzmo 20 years, 10 months ago
I was poking around on these forums and it seems people discourage the use of global variables. I was wondering how come? I used some in a project of mine. I am still very new to C++, so this project was just to see if I coudl put what I had just read together into something useful. My project was a text-based RPG battle type game. I had seperate .cpp fikes for the battle and the shop. There were a number of values I felt needed to be carried over to each section of the program. Basically, they were money, health, and power(how much damage the player could do per turn). So, I used global variables. Is this bad or is it just a situation where global variables are necessary? I am not sure whether of not the source code would help, but I do not have it on this computer. If you think it may help I can get it tommarrow and post it. Thanks, Chris
Advertisement
Go back and try to add a second character to the game, keeping track of each character''s seperate money, health, and power. Then, you''ll begin to see one of the problems with global variables.
quote:Original post by kdogg
Go back and try to add a second character to the game, keeping track of each character''s seperate money, health, and power. Then, you''ll begin to see one of the problems with global variables.


hehe that''s a lot of stuff to keep track of isn''t it?

If you haven''t done so already I would recommend that you at least group all those stats/atributes/hp/whatnots into a struct and then you can just create as many chars as you want using that struct. Perhaps even better you can create a class just for the character.

--{You fight like a dairy farmer!}

Sometimes global variables are neccesary. for example I have to have all my SDL surfaces global as they are referenced from different bits, and youy can''t have one type inside another, according to the error messages I got when I tried.
There''s some things that need it, but don''t store stuff like player stats etc global, and if you do, put it in a class or struct.
quote:Original post by witty
Sometimes global variables are neccesary.

Are they?


AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Global variables are one way of getting things to work, but there is supposedly always a non-global alternative.

If you''re not careful with it, you could design yourself into a corner and have to redo some things. That''s true for using globals or not.
It's not what you're taught, it's what you learn.
Global variables aren''t necessarily "bad". There a few reasons why they''re discouraged. First because you don''t know who else is messing with your data. By "who else" I mean other functions. With global data that''s used a lot, tracking down sporadic values can be a pain. Global variables are also bad when you''re working on a project, since you don''t know what global variables they are using, and vica versa. That''s not really much of an issue any more.
I prefer to stay away from them because they make code harder to read.

This topic is closed to new replies.

Advertisement