<div class="IPBDescription">1 Variable, More Than 1 File</div> Say I've got int date in time.cpp, and I want to get to it in main.cpp and change it. Is the only way to go about doing this using a function that returns a pointer to date?
EDIT: Ok, my advice is... don't worry about multipule files until you get to "classes". They aren't that neccisary until then. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--> Heh, I'm already 90% of the way through classes :X
I know a decent amount of C++, I just encountered this problem, which I've been using the function for. It's messy though, and it seems odd that there wouldn't be a simpler way to do this.
Is there a way to 'include' variables? I want to use them as if I had defined them in the same file, but I'm defining them in a different one.
<!--QuoteBegin-Blobby+Aug 18 2004, 04:03 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Blobby @ Aug 18 2004, 04:03 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Is there a way to 'include' variables? I want to use them as if I had defined them in the same file, but I'm defining them in a different one.<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--> Been there, devised the following solution:
#if CPPFILENUM == 1 int date; int minutes; int hours; #else extern int date; extern int minutes; extern int hours; #endif<!--c2--></td></tr></table><div class='postcolor'><!--ec2--> <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->time.cpp
// We tell time.h that we are currently in time.cpp by setting a unique file ID #define CPPFILENUM 1 #include "time.h"
Requires knowledge of the extern keyword and preprocessor definitions.
PS: You can easely do it more elegant but this is the general idea. PS2: Not entirely sure extern twice can trigger a redefinition error, but you can indeed put header guards around the whole header. PS-Fieari: Read the original PS more closely lad <!--emo&:)--><img src='http://www.natural-selection.org/forums/html//emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->.
extern is... weird. Basically, it tells the compiler that the variable in question WILL be defined later, or possibly has already been defined, and to use that value. It means that the implementation is in another file, basically.
If you have your #includes in the right order, and it's a global variable/function/class/whatever, it's not actually needed, since the compiler actually sees all the files as one file... if the declaration has already been made, it knows the thingy exists and can access it. The trick is when you have classes to refer to each other... they can't BOTH be above the other one... that's where externs can -really- be helpful. Also when you have thousands of seperate files and organizing them is too much trouble.
Edit2: Nevermind edit1, if you saw it.
Travis: That is really... ugly. Looks like a monster of a kludge. Wouldn't it be more elegent to just include time.h once and let it trickle down?
I work with classes mostly, and I've always found it easier to just keep the declarations and implimentations in the same file and include that file whenever it is needed. Basically it looks something like this:
The preprocessor commands will ensure that it doesn't get included more than once. This works great for non-class files too so long as you keep variables out of the global area of the file unless they are meant to be global for everything. Also with this method, extern is unnessesary, since the entire program gets compiled into one object file.
[WHO]ThemYou can call me DaveJoin Date: 2002-12-11Member: 10593Members, Constellation
<!--QuoteBegin-_Creep_+Aug 19 2004, 12:09 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (_Creep_ @ Aug 19 2004, 12:09 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Why do you want globals anyway ? Almost always you don't need them. Horrible things. Get out of control so easily and are a nightmare to debug. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd--> the common phases of a programmer and their use of globals.
1. no knowledge of globals, everything passed by values. Lots of temperature functions. 2. Globals found, everything is global, even useless counters. 3. Book/Instructor/Friend states that globals are bad, everything moves to pointers 4. programmer learns that globals are useful if tempered properly and uses them sparingly.
Seriously, for games or windows apps, you really can't get away without globals unless you want everything to be far more complicated than it needs to be.
Externed globals are emanations from Satan's bottom. They are the cause of many an evil bug in a multi-developer environment. You can't exert any control over who sets them. You may as well make copies of your front door key and give them to everybody on your street saying: "now you won't nick my TV will you ?".
At least use static globals in the .cpp only. You can exert a modicum of access control using functions since statics can't be externed.
As Them said, globals can be useful, when used properly. But try not to go overboard with them.
My gut feeling is still that, really, it's better to try to find another way of doing things. Having variables floating around loose isn't great. Putting them in some kind of namespace is better. Putting them in a class and having an instance of that class floating around at global scope is probably better still. I'm being tentative because the only solid rule about design is that, given the right circumstances, no rule is solid.
But my own view is that encapsulation is God, and leaving variables around for anyone to trip over is a Very Bad Thing Which Must Be Avoided At All Costs.
[WHO]ThemYou can call me DaveJoin Date: 2002-12-11Member: 10593Members, Constellation
<!--QuoteBegin-SoulSkorpion+Aug 19 2004, 06:01 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (SoulSkorpion @ Aug 19 2004, 06:01 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Putting them in a class and having an instance of that class floating around at global scope is probably better still. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd--> That works, but you just sacrificed performance for code organization.
I've recently started to use an amalgamation of a nested class with entirely static variables and static functions. And for any variables I don't want other people to be able to change, I made a nested namespace to correlate to the class, and in the class, I make a const reference to the variable that's changeable. Const reference lets the variable still be changeable but cannot be changed without explicitly casting away the const.
And if you're working with someone that will explicitly cast away a const just willy nilly. You really need to break their coding fingers.
<!--QuoteBegin-_Creep_+Aug 19 2004, 05:13 AM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (_Creep_ @ Aug 19 2004, 05:13 AM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> Externed globals are emanations from Satan's bottom. They are the cause of many an evil bug in a multi-developer environment. You can't exert any control over who sets them. You may as well make copies of your front door key and give them to everybody on your street saying: "now you won't nick my TV will you ?".
At least use static globals in the .cpp only. You can exert a modicum of access control using functions since statics can't be externed.
But then, you're getting pretty close to classes here anyway.
I'm not totally against globals. But in general, I will only use them for const data types. I am against externeable globals. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd--> Using functions for globals is what I've been doing already, it's disgusting and I hate it!
:X
I'm not quite sure of the difference between two functions that do simple variable manipulation and just using the extern keyword to get the same variable in your file for simple manipulation.
Comments
(ask a vague question, get a vague answer)
EDIT: Ok, my advice is... don't worry about multipule files until you get to "classes". They aren't that neccisary until then.
int date=1882004;
// manipulation etc...<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->main.cpp:
extern int date;
// manipulation etc...<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
No idea if this is what you mean...
(ask a vague question, get a vague answer)
EDIT: Ok, my advice is... don't worry about multipule files until you get to "classes". They aren't that neccisary until then. <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
Heh, I'm already 90% of the way through classes :X
I know a decent amount of C++, I just encountered this problem, which I've been using the function for. It's messy though, and it seems odd that there wouldn't be a simpler way to do this.
Is there a way to 'include' variables? I want to use them as if I had defined them in the same file, but I'm defining them in a different one.
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
time.cpp:
int minutes = 5;
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
main.cpp:
time += 5;
cout << time;
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
As an example.
EDIT: I've seen the 'extern' keyword before, but my book hasn't explained it. I have no idea what it does...
I'll look for an article on it or something :X
Been there, devised the following solution:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->time.h:
#if CPPFILENUM == 1
int date;
int minutes;
int hours;
#else
extern int date;
extern int minutes;
extern int hours;
#endif<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->time.cpp
// We tell time.h that we are currently in time.cpp by setting a unique file ID
#define CPPFILENUM 1
#include "time.h"
// Perform manipulations...
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->main.cpp
// Same as stated earlier, we'll use 2 for main.cpp
#define CPPFILENUM 2
#include "time.h"
// Perform manipulations...
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Requires knowledge of the extern keyword and preprocessor definitions.
PS: You can easely do it more elegant but this is the general idea.
PS2: Not entirely sure extern twice can trigger a redefinition error, but you can indeed put header guards around the whole header.
PS-Fieari: Read the original PS more closely lad <!--emo&:)--><img src='http://www.natural-selection.org/forums/html//emoticons/smile-fix.gif' border='0' style='vertical-align:middle' alt='smile-fix.gif' /><!--endemo-->.
If you have your #includes in the right order, and it's a global variable/function/class/whatever, it's not actually needed, since the compiler actually sees all the files as one file... if the declaration has already been made, it knows the thingy exists and can access it. The trick is when you have classes to refer to each other... they can't BOTH be above the other one... that's where externs can -really- be helpful. Also when you have thousands of seperate files and organizing them is too much trouble.
Edit2: Nevermind edit1, if you saw it.
Travis: That is really... ugly. Looks like a monster of a kludge. Wouldn't it be more elegent to just include time.h once and let it trickle down?
Fieari, I had my HWND defined at the global scope in main.cpp, but I tried to use it somewhere else, no go. :X
But extern is the coolest thing I've learned so far.
So very nifty! <3
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
#ifndef THISFILE_H
#define THISFILE_H
class Object
{
protected:
Uint32 variable;
public:
Object(void)
{
}
~Object(void)
{
}
};
#endif
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
The preprocessor commands will ensure that it doesn't get included more than once. This works great for non-class files too so long as you keep variables out of the global area of the file unless they are meant to be global for everything. Also with this method, extern is unnessesary, since the entire program gets compiled into one object file.
the common phases of a programmer and their use of globals.
1. no knowledge of globals, everything passed by values. Lots of temperature functions.
2. Globals found, everything is global, even useless counters.
3. Book/Instructor/Friend states that globals are bad, everything moves to pointers
4. programmer learns that globals are useful if tempered properly and uses them sparingly.
Seriously, for games or windows apps, you really can't get away without globals unless you want everything to be far more complicated than it needs to be.
At least use static globals in the .cpp only. You can exert a modicum of access control using functions since statics can't be externed.
fred.cpp:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
static int nTheNumber = 0;
int GetTheNumber() { return nTheNumber; }
void SetTheNumber(int _nTheNumber) { nTheNumber = _nTheNumber; }
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
fred.h
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
int GetTheNumber();
void SetTheNumber(int _nTheNumber);
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
But then, you're getting pretty close to classes here anyway.
I'm not totally against globals. But in general, I will only use them for const data types. I am against externeable globals.
My gut feeling is still that, really, it's better to try to find another way of doing things. Having variables floating around loose isn't great. Putting them in some kind of namespace is better. Putting them in a class and having an instance of that class floating around at global scope is probably better still. I'm being tentative because the only solid rule about design is that, given the right circumstances, no rule is solid.
But my own view is that encapsulation is God, and leaving variables around for anyone to trip over is a Very Bad Thing Which Must Be Avoided At All Costs.
That works, but you just sacrificed performance for code organization.
I've recently started to use an amalgamation of a nested class with entirely static variables and static functions. And for any variables I don't want other people to be able to change, I made a nested namespace to correlate to the class, and in the class, I make a const reference to the variable that's changeable. Const reference lets the variable still be changeable but cannot be changed without explicitly casting away the const.
And if you're working with someone that will explicitly cast away a const just willy nilly. You really need to break their coding fingers.
At least use static globals in the .cpp only. You can exert a modicum of access control using functions since statics can't be externed.
fred.cpp:
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
static int nTheNumber = 0;
int GetTheNumber() { return nTheNumber; }
void SetTheNumber(int _nTheNumber) { nTheNumber = _nTheNumber; }
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
fred.h
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
int GetTheNumber();
void SetTheNumber(int _nTheNumber);
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
But then, you're getting pretty close to classes here anyway.
I'm not totally against globals. But in general, I will only use them for const data types. I am against externeable globals. <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
Using functions for globals is what I've been doing already, it's disgusting and I hate it!
:X
I'm not quite sure of the difference between two functions that do simple variable manipulation and just using the extern keyword to get the same variable in your file for simple manipulation.
And I'm in a single developer studio :X