When a non-const variable is defined in the global scope, it can be accessed throughout the application. If it is a const variable, it is only a local variable in the current file.
We know that if you want to use a variable in other files, we can use extern to identify the variable. The default non-const type variable is extern.
1. cpp
Extern int number; // declares a number variable, which is used to search for definitions in other modules.
Int main (){
Cout <number <Endl; // The number is 100
}
2. cpp
Int number = 100; // defines a global variable. The default value is extern.
If we use const
2. cpp
Const int number = 100; // is this row? If the const fails, the local variable is not extern by default, but the local variable of the file. Therefore, you must add extern.
Extern const int number = 100; // This OK
1. cpp
Extern const int number; // we know that const must be initialized, but if extern is declared, no Initialization is required.
Int main (){
Cout <number <Endl; // The number is 100
}
The const object is the local variable of the file by default.