I am learning C ++, but this road is still under construction. After reviewing the basic knowledge over the past few days, I have not thoroughly understood the definition of extern in C ++ primer and discussed it with my friends, so we plan to practice it to help us understand it. Let me talk about how we feel in practice ~
Reference the description of extern in C ++ primer:
The extern Declaration is not a definition and no space is allocated. In fact, it only indicates that the variable is defined elsewhere in the program. Variables in the program can be declared multiple times, but can only be defined once.
Any variables used in multiple files must be declared separate from definitions. In this case, a file contains the definition of the variable, and other files that use the variable contain the declaration (rather than definition) of the variable ).
I can't help but have a few questions:
Next, I will first create a. h header file. The Code is as follows:
int a = 1;
Next, create the main. cpp file with the following code:
# Include <iostream> using namespace std; int main () {extern int a; cout <a <endl; system ("pause "); // used to pause the window to see the output result return 0 ;}
The output is 1, indicating that variables can be defined during the extern declaration. The second problem is also answered. extern is cross-file and does not need to be referenced by the. h header file.
Next, we create the header file B. h and define an identical variable. The Code is as follows:
int a = 2;
Result: The Compiler prompts an error. The error message is:
1> main. obj: error LNK2001: the external symbol "int "(? A @ 3HA)
1> E: \ Program \ c ++ \ exercise \ cpptest \ Debug \ cpptest.exe: fatal error LNK1120: 1 external command that cannot be parsed
I personally think the cause is that the compiler does not know which variable a to reference. We must instruct the program to reference a variable.
The solution is to reference the required header file.
Finally, we define a variable a in the int main () function, and other code remains unchanged. The Code is as follows:
# Include <iostream> using namespace std; int main () {extern int a; int a = 2; cout <endl; system ("pause "); // used to pause the window to see the output result return 0 ;}
The result is that the compiler prompts an error. The error message is "int a": redefinition.
In fact, the function of extern is to reference the definition in the header file so that it can reference the global variable a in other files, which is equivalent to turning the global variable into a local variable available in the function. If you delete this line of code "extern int a;", the output result is 2.
Conclusion: after my own practice, I finally understood what extern references are, and I also understood that primer only describes the meaning of variable definitions elsewhere in the program, many of the translations in these textbooks are very professional, which makes it difficult for beginners to understand the real purpose. Finally, please be careful with your skills. If you have any errors or comments, you are welcome to send a private message or comment.