Sign in to follow this  
orcfan32

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


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


Link to post
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;
}



header.h

extern bool globalVariable;

Share this post


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

Share this post


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


Link to post
Share on other sites
Fruny    1658
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
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_H

void myfunc(int &p_int);

#endif

otherfile.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_H

int myfunc(int p_int);

#endif

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


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


Link to post
Share on other sites
Lacutis    301
Quote:
Original post by orcfan32
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!


Well, you know, it's really hard to help you with your problems if you refuse to post code. Maybe if you post the code snippets that you added to try to do what we recommended we could help you out with the errors instead of giving you the answer. You see, the whole point in asking questions is to learn. If we just post all your code for you, and you copy it into your project, what are you learning?

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


Link to post
Share on other sites
Jonus    170
I use static variables in classes for stuff like this most of the time:
// .h
class global
{
public:
static int important_counter;
}

// .cpp
int global::important_counter = 0;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this