Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411476 Posts in 69369 Topics- by 58424 Members - Latest Member: FlyingFreeStudios

April 23, 2024, 04:50:00 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Global variables in a C++ project
Pages: 1 2 [3]
Print
Author Topic: Global variables in a C++ project  (Read 5346 times)
JakobProgsch
Level 1
*



View Profile
« Reply #40 on: September 05, 2013, 08:02:05 PM »

From my experience singletones aren't really better than just having the friggin globals in the first place. It's a pattern to enforce uniqueness. Not to justify globals. Sadly it is often used that way "I made it a singleton so it's ok!". In reality there are very few use cases where you really have to enforce uniqueness. Sure the typical use case of "TextureManager" objects is to have one... But there is no reason to enforce the typical use case.
I'm not sure why exactly, but the more I learn/gain experience the less I feel the need for globally accessible data anyway. Also the impulse to avoid them (and very "stateful" objects to begin with) comes from the realization that my code reuse comes almost exclusively from writing code that does not depend on any "state" be it global or not. Also it's a sure way screw up chances for nice paralleism, but i guess that is a minor concern for most.
Logged

InfiniteStateMachine
Level 10
*****



View Profile
« Reply #41 on: September 06, 2013, 07:56:44 AM »

Yeah if you plan to do unit tests, something like the service locator pattern can be a nice replacement for singletons. Or you could just not have unit tests.. Smiley

heh, I've gone this long without really touching them. I really should learn them just beucase TDD is getting popular.

That said I have trouble seeing their use in a game outside of a few special cases (perhaps that's a discussion for another thread though)
Logged

Orz
Level 1
*


"When nothing is done, nothing is left undone."


View Profile WWW
« Reply #42 on: September 06, 2013, 10:11:45 AM »

Usually when people argue about something this much, there's not much difference between the alternatives.  If there were, everyone would agree which was better.

I tried using extern variables but I am switching to singletons.  I was already putting my variables into their own files with init and cleanup functions, so it was more straightforward to just use a singleton.

@ThemsAllTook:  The strict singleton pattern sounds cool.  Do you have any examples?  I'm off to google it in case there are already a billion tutorials.
Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #43 on: September 06, 2013, 10:55:16 AM »

The simple way is to use a private constructor. If you're in a language where you can't do that (pretty sure C++ can), your constructor would just have to check if the shared instance is already initialized and fail if it is.
Logged

Evan Balster
Level 10
*****


I live in this head.


View Profile WWW
« Reply #44 on: September 06, 2013, 12:35:18 PM »

I initialize several registries in static time.  (Before main() is called or any concurrent threads can spawn)  These generally describe other pieces of code in the application such as drivers, plugins and reflected classes, and it wouldn't make much sense to model any mutable state with them.  The registry is filled by code called from global-static-value constructors, and locked before program execution starts.

Those are the only "globals" I make use of in my programming, save for occasional (and usually temporary) quick-hacks.  That's a bit of an unusual use-case, but a good one to be aware of as it's extremely useful in making code "optional" in compilation.
Logged

Creativity births expression.  Curiosity births exploration.
Our work is as soil to these seeds; our art is what grows from them...


Wreath, SoundSelf, Infinite Blank, Cave Story+, <plaid/audio>
oahda
Level 10
*****



View Profile
« Reply #45 on: September 08, 2013, 09:47:47 PM »

tl;dr use the best method for the project in question

Small games don't need to have a pretty solution at all.
Logged

Pages: 1 2 [3]
Print
Jump to:  

Theme orange-lt created by panic