|
C / C ++ language variables into the global and local variables. On the basis of this division is visible range of variables or called scope.
1 local variables
Refers to the local variable is defined in {} variables, it is also within the scope of this range. Although the common local variables are defined in the body of the function, but also can artificially increase a pair of braces to define the scope of variables.
As follows:
void f ()
{
float x = 0;
{
int a;
}
}
Do not underestimate the scope of this problem, which influence than pure C ++ C larger. When the C language in a local variable goes out of scope, the compiler inserts a POP instruction takes to clean up variable stack space. In C ++, in addition to POP instruction, but also to call the destructor.
class MyClass
{
MyClass () {}
~ MyClass () {}
};
void f ()
{
{
MyClass a;
} // Here will be inserted into the C ++ compiler invoked ~ MyClass () code
do_someting ();
}
C ++ compiler target before the end of a scope inserted automatically call ~ MyClass () assembler code.
Local variable scope is enforced by the compiler, so that once the action occurs outside access, a compile-time error will help programmers to debug errors.
2 Global Variables
Scope of global variables is the entire project, which is to participate in all linked files are visible. This can cause a problem - name conflict. For example, the following projects have three source files main.c, 1.c, 2.c.
main.c
#include < stdio.h>
int main (int argc, char ** argv)
{
return 0;
}
1.c
int a = 1;
2.c
int a = 2;
Each file is compiled through, but will be given a link, because 1.c and 2.c use global variables of the same name. To this end, the global variable C language are given a very bad image. Even without the use of global variables doctrine prevailed over a wide range.
However, global variables in many cases it is necessary, at least it makes the problem becomes easy to use. For example, when a variable is a function of many parameters.
void f1 (int a);
void f2 (int a);
void f3 (int a);
Thus each function call is required to pass the variable a, when the increase in the number of such parameters, will make people become mad. Such as
void f1 (int a, int b, int c, int d, int e);
void f2 (int a, int b, int c, float g);
void f3 (int a, int b, int c, int d);
In this case, you need to save the state of the program are common, such as GDI libraries, OpenGL libraries. In this case the use of global variables to maintain state data is a very good choice. C ++ see this need, it is simply the state of these data and algorithms function to bind together to form a class concept, which simplifies code design.
3 file-level variables
Many times, in fact, the visible range variable programmers need neither the whole project, nor is an internal function, but is visible in the current file. C language aims to provide a static global variable. static global variable. The name can not fully reflect the scope of the variable range, it is a very bad name. And from the static keyword it is confusing.
static int a = 100;
Perhaps the designers of C language is to save the use of keywords, the keywords used in many different places have a completely different meaning. This design should be the eyes of the beholder thing, I personally feel that if you use other keywords here identified as internal, it would be easier to understand.
internal int a = 100;
In C #, like the existence of similar keywords to denote scope.
Closer to home, static global variable modified only in the definition of its internal file is valid, it can not be referenced within other files. The above examples of the following:
main.c
#include
int main (int argc, char ** argv)
{
return 0;
}
1.c
static int a = 1;
2.c
int a = 2;
At this time, the project will be successful link. Because only a global scope called a global variable value of 2, a 1 is the only valid within 1.c.
4 C and C ++ compiler const constant bit different
C ++ compiler will automatically increase the static constant const keyword to the file level scope. The C compiler does not. The following code:
main.c
#include < stdio.h>
int main (int argc, char ** argv)
{
return 0;
}
1
1.c
const int a = 1;
1
2.c
int a = 2;
Using the C ++ compiler can be successfully compiled and linked successfully, but using the C compiler is being given at the time of connection. For code portability, it is best to manually write on the static const.
main.c
#include < stdio.h>
int main (int argc, char ** argv)
{
return 0;
}
1.c
static const int a = 1;
2.c
int a = 2;
The code is in C and C ++ compiler can compile and link successfully. |
|
|
|