Program Wide Global Vars in C++?

Started by
21 comments, last by Khatharr 11 years, 3 months ago

I've just recently learned from my program that "static" or pointer variables do not neccesseraly mean global program wide. I have a single .h file included for main .cpp and another .cpp file. When I changed the variable in main and read it in the other class it was not the same value as I've set it in the main class, even though it was declared static, or as I've tested even if it is a pointer, in the .h file.

Seems like the only way in C++ to have access to a variable you would like to use in another class is to pass it through a function or a constructor as well as using inheritance. As I recall in Java or C# a static variable or a function can be defined in one class and changed from a class while changing it for all the other classes.

It may be breaking the laws of OOP but I was just wondering if it was possible in C++.

Thank you, and have a nice day!!! smile.png

Advertisement

global.hpp

extern int counter;

global.cpp

include "global.hpp"

int counter;

other.cpp

include "global.hpp"

// Use counter.......

And, taking the wonderful example of Rod, if you don't feel like including a whole h(pp) file for a variable, you can extern declare said variable in any place that you want it.

i.e.

void BlaBluBli()

{

int a = 2;

extern int counter = a + 7;

}

As long as you ONLY declare it NON EXTERN once, every other external declaration will be solved by the compiler at link time.

But, DON'T! NEVER! Not even once! As an abuser of global variables, especially at work, especially when I need to do things quickly, I can tell you that usually it comes back to byte you (pun intended!). The hassle, when you decide to expand on your work, far outweighs any gains; usually this is a sign that your overall design has a few flaws in it.

Don't do it, EVER, I mean it :p (or if you do, don't let anyone else play with your globals :p )

Ah lol the extern, completely forgot it existed. I'll take your advice, but I like to make a mess also though, makes me look like I've been working : )

Thank you very much!!!

It may be breaking the laws of OOP but I was just wondering if it was possible in C++.

I don't feel like it is breaking the laws of OOP. It depends on what you need. Sometimes it is used as it is the most convienient approach. Please see info about singleton pattern. It is considered an alternative to global variables.

[quote name='Flopid' timestamp='1356749322' post='5015285']
It may be breaking the laws of OOP but I was just wondering if it was possible in C++.
[/quote]

C++ is a multi paradigm language, so it allows you to follow the "laws" of OOP, as well as a lot of other programming styles.

For values that have to be stored globally, using a global variable is a pretty good idea! ;)

[quote name='Misery' timestamp='1356782306' post='5015404']
Please see info about singleton pattern. It is considered an alternative to global variables.
[/quote]

Actually, the singleton pattern is sometimes misused as an alternative to global variables. In C++, types declared in header files have global scope. The purpose of the singleton pattern is to prevent a type from being instantiated more than once. This restriction should only be used if the type will stop working, not because the intent is to only instantiate one. However, it is usually better to design the type in such a way that it will allow multiple instances.

I hope I don't start a flame war here, please search on Google for multiple explanations why the singleton pattern shouldn't be used.

[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
Please see info about singleton pattern. It is considered an alternative to global variables.

Nooooo! The singleton pattern and global variables solve two different problems, and the people who use a singleton when all they really wanted was a global variable are likely just trying to horribly abuse objected oriented programming, claiming that because the global is wrapped up in a singleton class it's somehow "better," which is just plain wrong, wrong, wrong.

If you need a global, use a global. If you need a singleton, use a singleton. They are not equivalent alternatives. Yes, a singleton is, you could probably argue, a form of a global variable, but it's got extra baggage that globals don't. The reason people use singletons (or at least the reason they should use singletons) over globals is because they specifically wantneed that extra baggage. If you really have no need for that extra baggage, using a singleton is simply using the wrong tool for the job.

Now, people will argue whether or not you should use singletons and/or globals at all, and I'm not going to turn this into a debate over if one should never use them or not, but I will say that you should always try to minimize their usage. Passing things as parameters instead of using globals is easier to debug, easier to make your code thread safe and multithreaded, easier to follow the program flow and design, etc. so at the very least, try to minimize their usage. It's easy to make everything a global, but having a ton of globals will quickly turn your code into a spaghetti mess.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

I think most of our denizens are in agreement about the use of singleton... Even if everyone will argue about why they're in agreement.

lol

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

I actually am not very found of singletons; they can, as stated before, be easily abused to take a different role than they were designed. And, again, not a fan about "the role that they were designed for". You NEED one instance, I'm gonna trust that you are a big enough boy to instantiate it only once. You cannot? Oh, I have news for you... you're doing something wrong :) <insert rage replies here>

Also, I've met, more than once, naïve singleton implementation that did more wrong than right; for example in the function I wrote I needed to make sure that the said singleton was "destroyed" (the why's of me needing that are a whole different STUPID story, needles to say, I needed to), and simply checking if the "singleton" instance was != NULL would have the "nice" side effect of instantiating it if it was not instantiated before... WHICH WAS STUPID, error inducing and simply nerve wreaking. Me having to check the implementation of something that advertises itself as a singleton and modify it is not my idea of a fun time :)

Usually, through careful architecture design, no singleton would be implemented... ever... nor global variables. But, sure, as with everything else in C++, you want it, you could usually build/use it; but you usually pay the appropriate price for it

This topic is closed to new replies.

Advertisement