Program Wide Global Vars in C++?

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

Somewhat relevant: Unifying Global Variable Declarations & Definitions Using The Preprocessor. It's an old trick which can be useful as the list of global variables grows, but of course, if you have a long list of global variables, it's a sign that your design could use cleaning up.

Advertisement

Personally, as a thumb rule, I avoid using globals altogether. Globals can ruin many a days in debugging some bizarre issue, apart from other evils.

And using Singleton for global should also be the absolute last resort. Good design can very well just use the reference to one object across different classes. And that almost always addresses the problem of globals.

Only good example I can think of when to use globals is if you want to include some generic information in minidumps.

The OP is specifically talking about singletons. The title is just misleading. The suggestion to use singletons is void. The singleton has many ways lagging a project weeks behind. I remember one time when we had a templated singleton, that is static data member in header file. The result was that we had 2 different singletons in different translation units. This was the at the same time we we're battling about the construction and destruction order. Funny thing about destruction is, that if singleton object being destructed (after main() returns) refers to another singleton, and that singleton have already been destructed, the access function (class.instance() or such) recreates it. It's destructor is not gonna be called again.

My suggestion is to wrap the global parameter you want to RAII object, and while it's alive, global access is possible. Any access outside of the scope guarded object should throw exception or cause abort(). This way you can manage the lifetimes of everything, and any access to non-living objects are caught at development time.

I can give example of my suggestion if you need one (or other members here).

The OP is specifically talking about singletons.

No (s)he isn't. Flopid just talks about having static/shared (i.e. global) variables in the original post, but there is nothing that sounds at all like a singleton.

@Flopid: I'm not sure how you were using the static keyword, but it has different effects depending on where you use it. I wrote a little post on my blog about it which might explain things for you a bit. It's hard to give you a more specific response, seeing as I'm not sure how you were using static, but this might clear some things up. It's possible you were making a static global variable, which would indeed mean that each source file gets its own copy of this global variable, and changes made from one file are not reflected in others.

[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 ]

Wow a lot of info from all of the posts, just for a global variable. I am not completely sure how to implement a singleton yet, but I found this website. There are a few questions I have about this, but it is probably a subject for another thread. It seems as if a singleton is much better to use in a team environment, where people can accidentaly instantiate a global more than once, plus as I see it is more safe for threads. I'll have to reasearch more, but for now I'll probably just use "extern" as I can keep track of the code I have.

This bump is not intentional, just wanted to contribute to the conversation : )

I'd recommend reading those 'why singletons are evil' articles before you joyously leap into using them everywhere. There's some really good points made.
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.
It seems as if a singleton is much better to use in a team environment, where people can accidentaly instantiate a global more than once

If you have that many global variables, I'd say you have a problem. Even worse, it sounds like they're scattered all over the place. Using singletons in this case sounds more like putting duct-tape over a bullet wound instead of doing surgery, removing the bullet, and putting proper bandaging on.

plus as I see it is more safe for threads.

They're not. At all. There is nothing about a singleton that makes it more threadsafe than a global. Any thread safety you add to a singleton you must also add to a global. In fact, singletons may be even less safe (because they prevent another instance from being created and used).

This bump is not intentional, just wanted to contribute to the conversation : )

It's always good to come back and respond to threads :)

[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 ]
Using singletons in this case sounds more like putting duct-tape over a bullet wound instead of doing surgery, removing the bullet, and putting proper bandaging on.
[attachment=13056:wait_wut.jpg]
Been there.
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 usually do try to follow OOP without globals, but it seems when, for example, you have a variable that you have passed through a function or a constructor to many classes you pretty much have to somehow document it anyway, for code clarity. In this case it would seem like a global would be just a bit easier to manage. That, however, raises a question, why did you bite the bullet, and thus leaves the scope of this book.

I usually do try to follow OOP without globals, but it seems when, for example, you have a variable that you have passed through a function or a constructor to many classes you pretty much have to somehow document it anyway, for code clarity.

For what it's worth, passing parameters is more "self-documenting" than a global is. But if you find yourself sharing the same object in many classes, your classes probably aren't design correctly and following SRP.

But yeah, whether or not you should use globals and what alternatives there are to globals is a different topic which has been discussed many times. Getting into that huge mess wouldn't be very relevant to this thread's focus.

[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 ]

This topic is closed to new replies.

Advertisement