Jump to content
  • Advertisement
Sign in to follow this  
orcfan32

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

This topic is 4746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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;
}



header.h

extern bool globalVariable;

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
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;".


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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by ext
If you're using an object oriented approach the singleton pattern will interest you.


Uh-oh, you said the S-word.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!