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

namespace

Started by icecubeflower Jan 27, 2008 at 5:06 PM 6 replies 750+ views
Original Post
icecubeflower
icecubeflower
Hey what if you use global variables but then your .cpp file is getting unmanageable and you want to split it into several .cpp and .h files? I still want them all to use the same global variables. Is that possible? Would you use a namespace? What's a namespace?
janta
janta
uuugghh global variables [smile]
In order to access a variable defined in a compilation unit (a cpp file) from another compilation unit, you will need an "extern" somewhere. Either:

// .cpp 1int globalVar;// .cpp 2extern int globalVar;globalVar++;


Or with functions:

// .cpp 1int globalVarvoid IncrementVar(){    globalVar++;}// .cpp 2extern void IncrementVar(); // extern is implicit, usually nobody writes it explicitlyIncrementVar(); 


Throw in a few classes and replace global variables by dynamically allocated objects (or have a container handle that for you) and your code's organization should begin to look a bit more decent.
Zahlman
Zahlman
Quote:
Original post by icecubeflower
Hey what if you use global variables but then your .cpp file is getting unmanageable and you want to split it into several .cpp and .h files? I still want them all to use the same global variables. Is that possible? Would you use a namespace? What's a namespace?


In addition to what others said:

The global variables likely are a big part of why the .cpp file is "getting unmanageable". Could we see the code? I want to get a sense of what you're using them for and why you think you need as many of them as you do.
icecubeflower
icecubeflower
Hey thanks everybody I'm gonna do some more reading I think I get it.

I use global variables because global variables kick ass.
oler1s
oler1s
Quote:
I use global variables because global variables kick ass.
No. Just the opposite.
icecubeflower
icecubeflower
If I didn't use global variables I would have to pass about 50 variables to most of my functions. The only other thing I can think of would be to throw them all in a struct and pass the struct pointer around.
janta
janta
Begining of your project: global variables kick ass
End of you project: they get you ass kicked.

[grin]

Topic Locked

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

Sign in to reply to this topic.