MyClass GlobalVar;
int main()
{
MyClass VarInMain;
}
views:
807answers:
11The variable VarInMain is a local variable and can only be used inside the function where it is declared, in your case, the main function. The GlobalVar can be used in every function of your program because it was declared outside of a function. This is called Scope.
Scope. VarInMain can be accessed directly only by code in main. GlobalVar can be accessed directly by code in any function in the file.
A couple of things:
- Typically, they're allocated in different places. Local variables are allocated on the stack, global variables are allocated elsewhere.
- Local variables in main are only visible within main. On the other hand, a global variable may be accessed anywhere.
globals can be used in functions declared outside of main, while anything declared in main, must be passed to another function first.
VarInMain
is accessible only within the main()
function. If main()
calls another function, that function will not have access to it. This is function scope.
GlobalVar
is accessible in every function in the same file. If you put extern MyClass GlobalVar;
in a header, then it can be used in any function in files which have or include that declaration. This is global scope.
A simple example:
int y = 43;
void foo() {
// y is visible here, x is not
}
int main() {
int x = 42;
foo(); // x is visible here, but not when we enter the foo() function
}
A global variable is visible globally, across all functions. A local variable is visible in the scope in which it is declared only. if you declare a local variable inside main, it will be visible there, yes, but not in functions that are called from main.
More differences:
- If constructor/destructor of global object throws an exception, then function terminate is called and there is no chance to proceed. For local object, you can catch exception and do something (but it is still tricky to throw from destructor).
- Order of construction/destruction of global objects is not well specified. This means, that generally for two global objects you cannot say, which one is constructed first. From the other hand, local objects are created at point of defintion and destructed at end of block in order reverse to order of creation.
- Scope... (already mentioned)
In general, it is not a good practice to use global objects without very strong reasons to do so. Using of globals often leads to code which is hard to maintain.
Another difference: the order of global object initilization is undefined. For example:
static Integer Global_Integer(5);
static Foo Global_Foo;
As these are objects, the C++ runtime will call their constructors when initializing them, but we can't predict the order in which this will happen.
Another one: The global variables (or variables in any other namespaces) are initialized to (T)0
, with T
being the type of the variable (in case it's a simple non-class type), arrays and classes are initialized like that for all their elements.
But in my opinion, it's a good idea to explicitly initialize things anyway, and not rely on that zero initialization since it improves readability of the program when the initial value is explicitly mentioned. It's however still useful to know when reading someone else's code, always keeping in mind the compiler will initialize those automatically.
The local variable when it is not declared static isn't automatically initialized. So you have to do any initialization on your own in case T
has no constructor doing it for you. Always keep that in mind.
Another difference: Global variables will be initialized before the program starts (i.e. main() gets called) whereas the local variables are initialized as execution reaches that point (i.e. just after main is called in the above example).
A global variable is accessible to any function. A variable in main acts exactly like any other local variable, and is only accessible to code in main. Also, if main (or another function) calls itself, the local variables will be copied, but global variables will not.
int x = 0;
void myfunction()
{
x++;
printf("%i ",x);
}
int main()
{
myfunction();
myfunction();
}
This example will output: 1 2
Moving the "int x = 0;" into myfunction will output: 1 1
Because the local variable gets initialised each time myfunction is called.