global variables in c++
I was wondering how to create a variable in one file and then use it in all the files without the redeclaration of the variable with extern in every file that uses it.
I have seen this so I know that it is possible but I don't know how it was implemented.
Does anyone have a suggestion?
Make your global var in a .cpp file, then redeclare that variable in a header with "extern" before. Don't initalize it in the headerm though:
// in .cpp:
int my_int = 50;
// in .h:
extern int my_int;
Thanks for the quick reply. This was exactly what I was looking for :)