What is the method of directly assigning a value to an extern in c++?
In C++, you can use the keyword extern to declare an external variable, and then assign a value to this variable in another file. The specific method is as follows:
Firstly, use the ‘extern’ keyword in the file where the variable is being declared. For example, if we have an integer variable named ‘var’, it can be declared in the file like this:
extern int var;
After that, in the file where the variable is assigned, we can directly assign a value to it, for example:
int var = 10;
In this way, the variable ‘var’ is assigned a value of 10. When using this variable in other files, the value from the assignment file will be used.
It is important to note that the keyword “extern” is used only for declaring variables, not defining them. Therefore, variables need to be defined again in the assignment file.