Jump to content
  • Advertisement
Sign in to follow this  
derek7

how to handle global variable?

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

if I have a class D3D; and a variable D3D myD3D; this variable will be used all of program. how to handle this variable? like extern D3D myD3D,I don't like this sentence appear every file on my project. this will add complex .

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Your good friends K&R added a wonderful feature to C to solve your problem. Look into the #include directive. "But", you say, "I'm using C++, not C". In his wisdom, Stoustrup decided that #include was a good thing and allowed it in C++ as well.

Share this post


Link to post
Share on other sites
Expounding upon what the AP said, to have a global variable that you can share across translation units, you want to put the following into a header file. Say "globals.h"

extern D3D myD3D ;

Then, in the source file ("globals.cpp") you'd put the following.

D3D myD3D ;

Viola! Everywhere you include "globals.h", you will have access to myD3D.

Now, there are many who believe that globals are bad mojo. So alternatively, you might consider looking into makeing your D3D class a Singleton or Monostate. Then again there are those who believe using Singletons as surrogate globals is also bad.

Anywho, hope that helps.

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!