Use of extern and static in C ++

Source: Internet
Author: User
The use of extern and static in C ++ is confusing. The following is a summary: 1. the global variable definition is used to modify the global variable definition. The global variable definition is used to expand the scope of the global variable, other files can also use this global variable static to modify the definition of the global variable to limit that this global variable can only be used in this file. after the CPP file is compiled, it becomes. the function of the OBJ file extern is to make. OBJ can use B. the global variable static defined in obj is limited to B. the global variables defined in OBJ can only be in B. OBJ, cannot be. OBJ. For example, extern int A = 100; defines a global variable and expands its scope to the entire project. Static int A = 100; defines global variables and limits the scope of global variables to this module. For non-const variables, the default modifier extern is not added. For const variables, the default modifier staticextern const int A = 100 is not added. Note: when using extern to modify the definition of a global variable, you must write a full definition, such as extern int A (0) or extern int A = 0. Otherwise, extern int A is only a declaration for modifying variables. 2. the Declaration used to modify global variables is indicated in this. in the CPP file, the variable that will be used later is defined in the external file, so that. the CPP file can be compiled, and subsequent links will find the definition of the variable in other files of the project. Extern int A; 3. the Declaration used to modify a function is the same as that used to define and modify a variable. The function declaration and definition without any modifier are externnote by default: In the same project,. in CPP, extern int A = 5; B. CPP contains extern int A = 10; an error is returned when the link is connected: "int A" already defined in... if. in CPP, static int A = 5; B. if it is static int A = 10; or extern int A = 10; in CPP, there is no error. 4. during compilation, the extern "C" C ++ language will combine function names and parameters to generate an intermediate function name to solve the overload problem of functions with the same name. C will not, C does not support function overloading. Extern "C" is used to tell the compiler to generate the function name according to C, so that the C program can call the DLL written in C ++. You can simplify the use of extern "c" in the CPP file in the following ways:
# Ifdef _ cplusplus extern "C" {# endif code... # ifdef _ cplusplus} # endif
5. Mix extern "c" and extern & Static. Be sure not to be blinded by extern in extern "C". It does not contain the meaning when extern is used separately. If. in the CPP file, define a function to enable the compiler to generate the function name in the way of C and expand the function scope to the entire project. The following definition is used: extern "C" extern int test () {return 0;} is not difficult to understand the definition of the following function: extern "C" static int test () {return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.