• ### Announcements

#### Archived

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

# Best way for Globally Accesible Variables

## Recommended Posts

Shaun_30    122
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 on other sites
Arild Fines    968
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 on other sites
Shaun_30    122
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 on other sites
Lubb    122
quote:
The best way is to not have them at all...

-Um,, yea,,,, -in theory .
~

##### Share on other sites
Arild Fines    968
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 on other sites
Arild Fines    968
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 on other sites
Shaun_30    122
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 on other sites
Arild Fines    968
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 on other sites
Shaun_30    122
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 on other sites
Shaun_30    122
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 on other sites
Arild Fines    968

  struct Point{   float x;   float y;};//...void mouseMove( Point& pt ){   //...   pt.x = <something>   pt.y = <somethingelse>}

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 on other sites
Mulligan    378
I'm assuming that you know of classes, but probably don't use them. Using classes is the best way to get around having global variables, because the class members act like global variables within the class itself. Thus you could make a mouse class and give it Mouse X and Mouse Y members, as well as all the update code you need. It REALLY makes things easier if you take the time to learn how to use them, otherwise, it does get tricky to not have to use globals.

[edited by - Mulligan on October 20, 2002 8:00:21 PM]

##### Share on other sites
Shaun_30    122
Thank you so much! The thing I''ve always forgotten was the "&" sign at the declaration of the structure, making the usefulness of a structure limited to that of the scope of the function where it is declared. Now my project can move into a direction of organization I never thought possible. Thanks again.

##### Share on other sites
RapidStunna    122
You shouldn''t have all global variables and you shouldn''t have all member, local, or referenced, or whatever else I''m forgetting. There''s a limit. I prefer globals because they''re fast and easy to access. However they don''t work for everything. Too many global variables makes things confusing so there becomes a time when you need to store them in structures and classes. Also passing variables through function is helpful when you don''t know exactly what data should come in and out and need pointers to help you out.

---
Brent Gunning | My Site

##### Share on other sites
Shaun_30    122
Thank you Mulligan, again we go back to the fact I always forgot the "address of" sign when declaring structs (or in this case, classes) eliminating any usefulness a class would have for this situation. I am now considering a hybrid between yours and Arild Fines''s posts.

##### Share on other sites
micepick    122
I think you need to go over the basics of C++ again before you continue with your engine. You're forgetting really fundamental things, and if you don't remember basic C++ the structure and organization of your engine is really going to suffer.

EDIT: Arild Fines was not using an "address of" sign. It was a reference, which is somewhat different from a pointer.

[edited by - micepick on October 20, 2002 8:16:26 PM]