Summary of the C ++ qualifier

Source: Internet
Author: User

Connection: in an execution program, the identifier represents the bucket that stores variables or compiled function bodies. The connection describes the bucket in what the linker sees. There are two Connection Methods: internal link and external link:
The internal Link means only creating buckets for the files being compiled. With internal links, other files can use the same identifier or global variable, and the linker will not find any conflict-that is, create a separate bucket for each identifier. The internal links in the C and C ++ public are specified by the static keyword.
External links mean creating a separate bucket for all compiled files. Once a bucket is created, the linker must resolve all references to this bucket. Global variables and function names have external links. By using the keyword extern, you can access these variables and functions from other files. All variables defined outside the function (except const in C ++, const is an internal link by default, and clear declaration can be changed to an external link) and function definition. The default value is an external link. Local variables only exist in the battle, so there is no link.

1. Static
Static variables are used in one file. Only the code in this file can access them. Their names are invisible to other files. Space is allocated and initialized at the beginning of the program, and space is released at the end of the process. You can change the value during use. Static modification is not required for implementation, because static is a declarative keyword;
The static keyword is a keyword that exists in both C and C ++. It mainly has three usage methods. The first two are only used in C, the third method is used in C ++. (The detailed operations in C and C ++ vary. This document uses C ++ as the standard ).
(1) local static variables: Memory allocated in the global data zone, only initialized once, and the global life cycle
(2) external static variables/functions: global variables and functions that cannot be accessed by other files, but to limit the scope of global variables/functions,
(3) static data member/member function: variables and functions that belong to a class rather than any specific object of this class.
2. extern
Extern is a keyword in C/C ++ that indicates the range (visibility) of functions and global variables. It tells the compiler, the declared functions and variables can be used in this module or other modules.
1. For the extern variable, it is just a declaration of the variable, instead of defining the allocation of memory space. If the variable is defined multiple times, a connection error occurs.
2. In general, the function and global variables referenced by this module to other modules are declared with the keyword extern in the module header file. That is to say, the c file defines that if the function or variable is open to the outside, it will be declared with extern in the H file. Therefore, you only need to include the H file for external files. In addition, this function cannot be found outside of the compilation stage, but no error is reported. This function is found in the target code generated by the definition module in the link phase.
3. The keyword corresponding to extern is static. The global variables and functions modified by it can only be used in this module.

The following three keywords are related. const-volatile is also called a c-v qualifier.
3. volatile
Role of volatile: as a command keyword, make sure that this command is not omitted due to compiler optimization and requires that the value be read directly each time. Like const, volatile is a type modifier.
4. const
A variable (all variables, including member variables, parameter variables, local variables, global variables, and return values) cannot be changed. The most troublesome const limitation is the pointer limitation, which requires more attention. The keyword corresponding to const is mutable.
5. mutable
The non-static non-const data member used for the class, which means it is not restricted by the const of the member function and can be used in the const member function.

Other function qualifiers for next interview
6. inline
Inline only affects the compilation behavior of functions in the translation unit (which can be simply understood as the C source code file). As long as the inline attribute is out of this range, it does not have any effect. Therefore, the inline keyword should not appear in the function declaration. It does not have any effect. Sometimes it may cause compilation errors. The inline keyword is only recommended for the compiler to perform inline expansion, rather than forcing.
7. explicit
This keyword modifies the constructor declaration, indicating the explicit Constructor (Template). The explicit constructor does not participate in specific overloading. C ++ 11 extends its usage from two perspectives. One is suitable for conversion functions (templates), similar to constructors, to avoid unnecessary overloading. The second is list initialization. Unless std: initializer_list is directly used, the explicit constructor is ignored by list initialization (in C ++ 98/03, explicit only makes sense to call the constructor for a single parameter, this restriction is broken here ).

Related Article

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: 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.