Skip to main content
GameDev.net gamedev.net
🔒 Locked

Passing 'Global' Variables

Started by Carradine Dec 3, 2001 at 2:34 PM 9 replies 600+ views
Original Post
Carradine
Carradine
I have created a class with a series of global variables, I have a main class, which initializes several other classes within itself, so it acts like the master class of the program. Unfortunately the global class variables are able to be changed during run time, so I cannot make them constant, so I was wondering what is the most efficient way to pass the global variables along to every other class from my main class? Right now I create the global class variable within my main class, then for every other class that gets initialized, I pass a pointer to the global class that the main class created, so those classes have a pointer to the global variables that the main class holds. If that was not incredibly confusing, I would like some suggestions. Thank you.
--------------------------Vantage: Greenlit on Steam / Independent Games Festival Submission http://www.crystaldragon.com
Mayrel
Mayrel
Let me see if I understand you correctly...

  
class Main
{
public:
members...
};

class SomeClass
{
public:
SomeClass (Main *mainClass);
};


You''ve got this? SomeClass wants to access members of Main, but you''d like to save on typing? I can see two obvious solutions.

5. Make the members of Main static, so you don''t actually need to create an instance of Main -- you''d access the ''foo'' member of Main with ''Main::foo''.

4. Create an instance of Main in a global variable (g_main, say) and have SomeClass access the members of main through that.

With both of these solutions, you don''t need to pass the main class to the constructor.



Just Plain Wrong
Narcus
Narcus
#1) Do you have all these classes in 1 file or are they in separate files? They should probably be in separate files.

#2) OK. If you really have GLOBAL variables, they are not part of your class. Classes have member variables, not global variables. To get access to global variables between files, you have to declare them as EXTERN in your other files. You can search the forums for "global variables", or maybe "extern" for more info on this.

#3) If you really have MEMBER variables (not globals), I think you should probably just pass them as parameters to the other classes whenever needed...?

And if you only have 1 file, globals can be accessed anywhere.. don''t even worry about passing them. Though this is bad design..
Narcusmy homepage:http://www.pcis.net/amenzies
Carradine
Carradine
Ok, I believe I just found a solution. All I simply did was this:

in globals.h:

class Globals
{
public:
members...
};

static Globals VGlobals;




All of that is in a separate file, then all I do is #include the file in any place that needs the global variables, but I thought a static variable declared like this only has file scope, yet I can access, and change the values of the members from anywhere in my program. Am I misinformed on how this static variable is defined?
--------------------------Vantage: Greenlit on Steam / Independent Games Festival Submission http://www.crystaldragon.com
AdmiralBinary
AdmiralBinary
No. Static has many forms. What Mayrel meant (i think) was that you should do this:

  
class Main
{
public:
static int nSomething;
static MemberFunc();
};

void main()
{
cout << Main::nSomething << endl;
Main::MemberFunc();
}

It''s useful for when you are only ever going to have one instance of the class and it is going to "exist" at ALL times.

Hope this helps...

---------------

I finally got it all together...
...and then forgot where I put it.
AdmiralBinary
AdmiralBinary
Basically static in this case means that when you declare the CLASS Main, u are also sort of declaring the OBJECT Main. I have confused myself, plz excuse me before i make an idiot of myself.

---------------

I finally got it all together...
...and then forgot where I put it.
Carradine
Carradine
Ah I see, in a sense they somewhat have a similar function, and I have tried both and they work. For now it seems that just including Global.h in any file that I need the global variables seems to me more effective right now, though using static members within my main class is just as effective. I will stick with using my global.h right now, and I may need to change it later. But I am going to relearn the definition of a static variable, since I seems I dont know it completely.

Thank you all for your help
--------------------------Vantage: Greenlit on Steam / Independent Games Festival Submission http://www.crystaldragon.com
JonStelly
JonStelly
What are you trying to accomplish in all this? As a general rule, OOP programmers dislike global variables. However, understanding that they are sometimes necessary evils, I would like to see them all as a part of a single structure / class.

If your variables are members variables of some class (CGlobal), then you have member variables of class CGlobal, and an instance of CGlobal is your only global object. If you''re only going to have one instance of the CGlobal as your global object, then just make all of the member functions / member variables static for the class and don''t create an instance of the class and let the static initialization take care of things. Of course if your goal is something else then just disregard all of this.
Carradine
Carradine
ah, the simplest answer is always the right one. Yes, I will do just that. At first I was trying to make it so I could only need to use it in certain areas of the code, but making all the members static, and just accessing them that way seems to be the best way.

Its funny how you have been coding for over 6 years, and it always seems like you have learned nothing.
--------------------------Vantage: Greenlit on Steam / Independent Games Festival Submission http://www.crystaldragon.com
Carradine
Carradine
I attempted to make my VGlobal class a global class, and I have made all members static members. The program compiles, but I get linking errors in every file that I #include the global class into, do I still need to make an instance of a class that has all static members?
--------------------------Vantage: Greenlit on Steam / Independent Games Festival Submission http://www.crystaldragon.com
ju2wheels
ju2wheels
You must have forgotten to initialize the static member data in your class after your class....

class SomeClass
{
public:
static int memberVar;
};

int SomeClass::memberVar = 0; //I think that is how its done,
//I can't remember right off the
//top of my head...

And no, or not that I know of, you don't need an instance of the class if it is all static members.


Edited by - ju2wheels on December 5, 2001 4:52:06 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.