C + + descriptors and qualifiers

Source: Internet
Author: User

Some C + + keywords called storage specifiers (storage class specifier) or cv-qualifiers (Cv-qualifier) provide some information about storage. Here is the store descriptor:
* Auto (no longer a specifier in c++11);
* REGISTER;
* STATIC;
* extern;
* Thread_local (c++11 new);
* mutable.
Multiple specifiers cannot be used in the same declaration, except thread_local, which can be used in conjunction with static or extern.
Before c++11, you can use the keyword auto in the declaration to indicate that the variable is an automatic variable, but in c++11, Auto is used for automatic type inference.
Before c++11, register was used to indicate the register store in the declaration, while in c++11 it indicated that the variable was explicitly indicated to be automatic.
The keyword static is used to represent internal linkage when used as a declaration for an entire file, and is used in a local declaration to indicate that the storage continuity of a local variable is static.
The keyword extern indicates that it is a reference declaration, which declares a reference to a variable defined elsewhere.
The Thread_local keyword indicates that the persistence of the variable is the same as the continuation of the thread to which it belongs. The thread_local variable is to the thread, as if the general static variable is to the whole program.
1.cv-Qualifier
Here is the CV qualifier:
* CONST;
* Volatile
The keyword const indicates that after the memory is initialized, the program can no longer modify it.
Keyword volatile indicates that the value of a program code may change even if it is not modified by the internal deposit element. For example, you can point a pointer to a hardware location that contains time and information from a serial port. In this case, the hardware (not the program) may modify the contents of it. or two programs may affect each other, sharing data. The purpose of this keyword is to improve the compiler's ability to optimize. For example, suppose the compiler discovers that the program uses the value of a variable consecutively in several statements, and the compiler might not let the program look up the value two times, but instead cache the value in the register. This optimization assumes that the value of the variable does not change between the word used. If you do not declare a variable to be volatile, the compiler will do this: Declare the variable as volatile, which is equivalent to telling the compiler not to do this optimization.
2.mutable
Now go back to mutable. It can be used to point out that even if a struct (or class) variable is const, one of its members can be modified. For example, the following code:
struct data
{
Char name[30];
mutable int accesses;
...
};
Const data Veep = {"Alaybourne Clodde", 0, ...};
strcpy (Veep.name, "moonlit"); Not allowed
Veep.accesses + +; Allowed
The const qualifier of Veep prohibits programs from modifying members of Veep, but access members ' mutable specifiers make access not subject to this limitation.
3.const
In C + + (but not c), the const qualifier has a slight effect on the default storage type. By default, the global variable is linked externally, but the const global variable has a linked type of internal. That is, in C + + it appears that the global const (as shown in the following code snippet) is like using the static descriptor.
const int fingers = 10; Same as static const int fingers = 10;
int main (void)
{
...
C + + modifies the rules for constant types, making it easier for programmers. For example, suppose you put a set of constants in a header file and use that header file in multiple files in the same program. Then, after preprocessing it to include the contents of the header file in each source file, all the source files will contain definitions similar to the following:
const int fingers = 10;
const char * warning = "wak!";
If the link to a global const declaration is external like a regular variable, it will be an error based on the single definition rule. That is, only one file can contain the preceding declaration, while other files must use the extern keyword to provide a reference declaration. In addition, only declarations that do not use the extern keyword can be initialized.
extern would be required if Const had external linkage
extern const int fingers; Can ' t be initialized
extern const char * warning;
Therefore, you need to use a set of definitions for a file, and other files to use another set of claims.
If for some reason the programmer wants a constant to be linked externally, the extern keyword can be used to override the default internal linkage:
extern const int states = 50; Definition with external linkage
In this case, you must declare it using the extern keyword in all files that use the constant. This differs from regular external variables, where you do not have to use the extern keyword when defining a regular external variable, but you must use extern in other files that use the variable. However, keep in mind that because a single const is shared among multiple files, only one file can initialize it.
When you declare a const in a function or code block, it is scoped to a code block, which is available only if the program executes code in the code block. This means that when you create a constant in a function or code block, you do not have to worry about its name being in conflict with a constant defined elsewhere.

C + + descriptors and qualifiers

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.