# Is there a such thing as a "Global Variable" in C++?

## Recommended Posts

orcfan32    100
I need to write a function in a separate-from-main .cpp file. I needs to change the value of a variable from the function, but if I put the variable in the function, it will be what I declared it's value in the function whenever I call it. I need a variable that can be changed by the function without setting it when I call the function. Any help? Using Dev-C++, Windows XP, [no code needed].

##### Share on other sites
TDragon    679
Chances are you'll see a few replies on the evils of globals, but...

C++ supports globals in the same way as C: place the declaration of the variable at the top scope in a source file, i.e. before any functions for easiest readability.

In the source file of the function you want to use it, place an extern line at the top scope. For instance, if you have "int my_global_int;" in the first file, use "extern int my_global_int;".

Voila.

-Twilight Dragon

##### Share on other sites
gunning    749
Hi orcfan32.

Yes, there are such things as global variables in C++. You want to declare your variable outside of the function in either .cpp file. In a .h file, declare the variable again but put the keyword extern before it. Have both .cpp files include the .h files. That will give global access to your variable.

Because examples can be particularly helpful...

foo.cpp
#include "header.h"bool globalVariable;void foo (){   globalVariable = true;}

bar.cpp
#include "header.h"void bar (){   globalVariable = false;}

extern bool globalVariable;

##### Share on other sites
Fruny    1658
Quote:
 Original post by TDragonIn the source file of the function you want to use it, place an extern line at the top scope. For instance, if you have "int my_global_int;" in the first file, use "extern int my_global_int;".

A more common though equivalent way would be to stick the extern int my_global_int; in a header file logically associated with the source file you put that definition in. So if int my_global_var; is in foo.cpp, you could have extern int my_global_var; in foo.h

##### Share on other sites
TDragon    679
Correct. Perhaps I should've used "compilation unit"? :D

##### Share on other sites
orcfan32    100
Thanks for the quick replies!

Anyways, I got it working!

##### Share on other sites
CraZeE    217
if interpreted differently, there is an alternative solution to the OP's situation. If it's just a matter of maintaining a persistent value that doesnt get reinitialized on each function call, you can just declare the variable as static within the function.

of course, if the title of this thread is correct then just ignore this method. Follow the pointers above for aid on using globals.

SiCrane    11839

##### Share on other sites
ext    180
If you're using an object oriented approach the singleton pattern will interest you.
It's a way to make variables/object global, but you still have the possibility to check who is accessing its value (makes debugging easier).

If I'm programming in C I avoid global variables by using functions to manipulate them, for example:

/* function that contains the "global" variable */int* get_global_int(){   static int global_int;   return &global_int;}void set_global_int( int val ){   int* global_int = get_global_int();   *global_int = val;}

If there's an error in my program all you have to do is to set a break point in set_global_int and to look for the bad piece of code.

##### Share on other sites
Fruny    1658
Quote:
 Original post by extIf you're using an object oriented approach the singleton pattern will interest you.

Uh-oh, you said the S-word.

##### Share on other sites
ext    180
I don't understand, is there anything with my advices?

##### Share on other sites
Lacutis    301
Theres even another option for the original poster.

He wants to call a function and have it able to change the original variables value. Sounds like a job for a reference to me.

main.cpp:#include <iostream>#include "otherfile.h"int main(){  int myint = 1;  myfunc(myint);  std::cout << "My Int : " << myint << std::endl;}otherfile.h:#ifndef OTHERFILE_H#define OTHERFILE_Hvoid myfunc(int &p_int);#endifotherfile.cpp:#include "otherfile.h"void myfunc(int &p_int){  p_int = 42;}

And of course there is ANOTHER option, which is to make the function return the modified value:

main.cpp:#include <iostream>#include "otherfile.h"int main(){  int myint = 1;  myint = myfunc(myint);  std::cout << "My Int : " << myint << std::endl;}otherfile.h:#ifndef OTHERFILE_H#define OTHERFILE_Hint myfunc(int p_int);#endifotherfile.cpp:#include "otherfile.h"int myfunc(int p_int){  if(p_int == 1)    p_int = 42;  else    p_int = 99;  return p_int;}

I would investigate all these options (mine and the others) before using a global.

##### Share on other sites
orcfan32    100
Ok, if I want to use it in lets say..Main.cpp and have 1 header file and use a global string, how would it be done?? I tried what you all said but I'm geting tons of errors!

##### Share on other sites
Lacutis    301
Quote:
 Original post by orcfan32Ok, if I want to use it in lets say..Main.cpp and have 1 header file and use a global string, how would it be done?? I tried what you all said but I'm geting tons of errors!

Instead, post the code snippets that relate to the problem (like I did) and what errors you are getting, and we can help you figure those out.

##### Share on other sites
Jonus    170
I use static variables in classes for stuff like this most of the time:
// .hclass global{public:   static int important_counter;}// .cppint global::important_counter = 0;