Jump to content
  • Advertisement
Sign in to follow this  
Bow_vernon

Ways to avoid the needs and the musts of global variables??

This topic is 2849 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

How do you do it? right now Im making a App class and put all program variables there. I know it might not be the best to do it, so I ask you here...
(anyway, might be good if one create a 'Useful C++ tips and tricks' thread here somewhere...just ideas though. Could help n00bs like me :p )

Share this post


Link to post
Share on other sites
Advertisement
You can pass variables by reference for starters. That way you can change the values if need be. And redesigning some of the program could help eliminate the need for global variables. If lots of different pieces need the information, then you may (or may not depending) be doing something a bit "wrong".

Share this post


Link to post
Share on other sites
If there are objects/class that a lot of code references, then your design could use improvement. More specifically your classes should be more loosely coupled.

Share this post


Link to post
Share on other sites
Im sorry c_olin, but I dont understand the "loosely coupled" part. What do you mean by that?
@Dragonsoulj : what if for example, almost all part of my program need acces to a Logger object, is it wrong?Well I already made it a singleton, so it works out Okay.

Share this post


Link to post
Share on other sites
The simple answer is it depends. No matter how much you may not accept that as an answer it is it. As you gain more experience you will become more familiar with the points at which you should employ certain techniques, and that's the bottom-line. The only broad generalization that can be made is that no broad generalizations can be made.

Share this post


Link to post
Share on other sites
Why not have a Logger object that is a part of another class? Just declare one either statically or as a private/public/protected variable and work with it that way? Or allow functions to accept Logger objects? Or reverse the thinking and have the other functions called from the Logger object?

Share this post


Link to post
Share on other sites
Quote:
Original post by Halifax2
The simple answer is it depends. No matter how much you may not accept that as an answer it is it. As you gain more experience you will become more familiar with the points at which you should employ certain techniques, and that's the bottom-line. The only broad generalization that can be made is that no broad generalizations can be made.


QFT.

Edit: And that probably applies for everything else in life.

Share this post


Link to post
Share on other sites
@DragonSoulj : well I like the way singleton works and Im ok with it now
And thanks for the suggestion, I think wrapping an application in a class and putting all the needed variables there is the way to go. The guy from the Black and White told me that he did just the same. Now, Back to coding!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!