Hi I want to know how can I use a global variable, I used the public statement but it doesn't work.
Your question isn't sequitur. The public c++ keyword enables code outside of a class to see the functions and data of a class.
i.e.
class Foo
{
public:
void TheWorldCanSeeMe(int aVariable);
int _theWorldCanSeeMeToo;
};
To create and use a global variable, which in general is not a good idea, but every once in a blue moon is most expedient, declare in a header file:
We will call this globals.h.
extern int gMyGlobalVariable;
Anything that wants to USE the variable will include the globals.h header file.
In a file named, global.cpp, you will define the variable:
int gMyGlobalVariable(0); // It is a good idea to initialize it to a known value.
So, any code that has included globals.h can access that variable. i.e.
gMyGlobalVariable = 3;
You can make classes global as well. Again, make certain you need to. There is ALOT of penalty for choosing a global variable.
In the cpp