Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Shaun_30

Best way for Globally Accesible Variables

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

What''s the best way to do have globally accessible variables in C++? Endless amounts of externs in a header file making every little change to the header a recompile of every document using it, or is there a better way through the use of OOP (which im new to)?

Share this post


Link to post
Share on other sites
Advertisement
The best way is to not have them at all...



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
The main reason I need them is for the sake of orginzation... for example I have a function that draws a mouse icon onto the engine im making. It needs the mX (mouse X) and mY (mouse Y) to draw it correctly, I have this moved into its own *.cpp file. Now you''d think for a solution I could have this local in said *.cpp file, but what about when I implement mouse click detection, will it not need these points (actually I know it needs these because its already implemented), not to mention mX and mY''s dependenices in the Main.cpp file. Now I could lump everything into one big file and everything would be ok right? Well, besides for the added compile time, looking time for specific areas, general organization, and the confusing necessary use of protoype declarations im gonna need, but those are onjly little things, right?

Share this post


Link to post
Share on other sites
It''s not theory for me...



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
quote:
Original post by Shaun_30
...


You know that functions take parameters, yes...?





The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
You know that functions take parameters, yes...?


Yes, but can you tell me how this can help me?

EDIT: How I can have a typo in such few words I'll never know...

EDIT #2: Oh, I bet you intend for me to make a function take the mX and mY params. pseudo function being...
void MouseMove(float *MouseX, float *MouseY);
...correct?
well if so then thats fine and dandy, but what I was implying was I have many instances where I need global variables, but if what above is the kind of trick that game programmers use to overcome this problem then I guess this is how I could do it...answer please...

[edited by - Shaun_30 on October 20, 2002 7:03:28 PM]

[edited by - Shaun_30 on October 20, 2002 7:12:13 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Shaun_30
EDIT #2: Oh, I bet you intend for me to make a function take the mX and mY params. pseudo function being...
void MouseMove(float *MouseX, float *MouseY);
...correct?


Yeah. Or you could use references instead of pointers. Or even make the point an aggregate structure, and pass a pointer/reference to that.

quote:

well if so then thats fine and dandy, but what I was implying was I have many instances where I need global variables,


No, you don''t. You have many instances where you think you need global variables. Globals are never necessary,although I guess there could be circumstances in which their use was preferrable to the alternative.

Personally, I can''t remember the last time I used a global in something I wrote.
quote:

but if what above is the kind of trick that game programmers use to overcome this problem then I guess this is how I could do it.


Trick??? Parameter passing is a very fundamental aspect of almost any language.



The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)

Share this post


Link to post
Share on other sites
I''m more comfortable with pointers although I''ll take a look at what you suggested...by trick I didn''t mean something used cheaply to get something done, but as something those who have more experience use to get something done (which is pretty much what you said)...time for a complete background overhaul for my engine-in-progress...thank you for your input .

Share this post


Link to post
Share on other sites
Actually do you think that you could show me an example of utilizing an aggregate structure and passing a pointer or reference in this type of a situation? My quick-search on google has yielded no fruitful results.

Share this post


Link to post
Share on other sites

  • 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!