C++ Variable Scope
Generally speaking, there are three places where variables can be defined:
Variables declared inside a function or a code block are calledLocal variables。
Variables declared in the definition of function parameters are calledFormal parameters。
Variables declared outside all functions are calledGlobal variables。
Scope is an area of a program. The scope of variables can be divided into the following types:
Local Scope: Variables declared inside a function have local scope; they can only be accessed inside the function. Local variables are created each time the function is called and destroyed after the function finishes execution.
Global Scope: Variables declared outside all functions and code blocks have global scope; they can be accessed by any function in the program. Global variables are created when the program starts and destroyed when the program ends.
Block Scope: Variables declared inside a code block have block scope; they can only be accessed inside the code block. Block-scope variables are created each time the code block is executed and destroyed after the code block finishes execution.
Class Scope: Variables declared inside a class have class scope; they can be accessed by all member functions of the class. The lifetime of class-scope variables is the same as the lifetime of the class.
Note:If a variable declared in an inner scope has the same name as a variable in an outer scope, the variable in the inner scope will override the variable in the outer scope.
Local variables
Variables declared inside a function or a code block are called local variables. They can only be used by statements inside the function or code block. The following example uses local variables:
Example
Global variables
Variables defined outside all functions (usually at the head of the program) are called global variables. The values of global variables are valid throughout the entire lifetime of the program.
Global variables can be accessed by any function. That is, once a global variable is declared, it is available throughout the entire program. The following example uses global variables and local variables:
Example
In a program, local variables and global variables can have the same name, but within a function, the value of the local variable will override the value of the global variable. Here is an example:
Example
When the above code is compiled and executed, it produces the following results:
10
Initializing local variables and global variables
When a local variable is defined, the system does not initialize it; you must initialize it yourself. When a global variable is defined, the system automatically initializes it to the following values:
| Data types | Initialize default values |
|---|---|
| int | 0 |
| char | '\0' |
| float | 0 |
| double | 0 |
| pointer | NULL |
Correctly initializing variables is a good programming practice; otherwise, the program may produce unexpected results in some cases.
Block scope refers to variables declared inside a code block:
Example
int main() {
int a = 10;
{
int a = 20; // block scope variable
std::cout << "Block variable: " << a << std::endl;
}
std::cout << "External variable: " << a << std::endl;
return 0;
}
In the above example, a variable named a is declared in the inner code block, which has the same name as the variable a in the outer scope. The variable a in the inner scope will override the variable a in the outer scope. When accessing a in the inner scope, it outputs 20, while when accessing a in the outer scope, it outputs 10.
When the above code is compiled and executed, it produces the following results:
块变量: 20 外部变量: 10
Class Scope
Class scope refers to variables declared inside a class:
Example
class MyClass {
public:
static int class_var; // class scope variable
};
int MyClass::class_var = 30;
int main() {
std::cout << "Class variable: " << MyClass::class_var << std::endl;
return 0;
}
In the above example, a class-scoped variable named class_var is declared in the MyClass class. You can use the class name and the scope resolution operator::to access this variable. When accessing class_var in the main() function, 30 is output.
类变量: 30other extensions