C ++ keyword (staticregisteratuexternvolatileconst)

zhaozj2021-02-16  45

Below the next few keywords from C are often dealing with us, and we often have a vaguely, this article is based on its own learning experience, in order to achieve truly understanding and purpose.

Static

l Static variable scope In one file, the program is assigned a space, and the space is released at the end. The default is initialized to 0, and its value can be changed when used.

l Static variables or static functions, that is, only code within this document can access it, its name (variable name or function name) is invisible in other files.

l The value of static variables generated in the function can only maintain

INT MAX_SO_FAR (INT CURR) // Since the time (this call) is maximum

{

Static int biggest; // This variable maintains the latest value at each call, its validity period equal to the validity of the entire program

IF (Curr> Biggest)

Biggest = CURR;

Return Biggest;

}

l The member variable of the C class is declared as static (called static member variable), which means that it is shared for all instances of this class, that is, when a class instance modifies the static member variable, its modified value See all other instances of such classes; the static member functions of the class can only access static members (variables or functions).

The static member variable of the L class must be initialized within the scope of its file, and the private type is no exception. Such as,

Float savingsaccount :: currentrate = 0.00154;

(Note: Currentrate is a static member variable of Savingsaccount)

Register

l Specify the register variable with the register declared, in the possible case, it will be stored directly in the machine's register; but it does not work for the 32-bit compiler, when Global Optimizations is opened, it will make Choose whether to place it in your own register; but other symbols related to the Register keyword are valid for 32-bit compilers.

Auto

L. It is a storage type identifier, indicating that the variable (automatic) has a local range, a variable declaration of block range (such as a variable declaration in the FOR cycle) defaults to the auto storage type.

Extern

l Declaring variables or functions are external links, that is, the variable or function name is visible in other files. The variable (external variable) of its modified variable (external variable) is a static allocation space, that is, the program starts to assign, and release it at the end. Use their declared variables or functions to be defined elsewhere else or otherwise elsewhere (implementation). Declaring a variable or function default to the file can be used outside.

l In C , it can also be used to specify a link to use another language, and you need to use with a specific transition. At present, Microsoft C / C only supports "C" conversion tags to support C compiler links. There are two forms using this situation:

u extern "C" declaration statement

u extern "c" {declared sentence block}

Volatile

l Defining an object can be changed by an external process (operating system, hardware or concurrent thread, etc.), as follows:

Int Volatile NVINT;

Such a statement cannot achieve the most efficient because their value changes in any time, and the system will read and write this object frequently when needed. It is often used to perform memory unit access as an asynchronous process like an interrupt handler. Const

l Constly modified objects or variables cannot be changed, and when the modification function, the function cannot be changed to the variables declared outside of the function cannot call any non-Const functions. In the last parentheses of the function parameter list, in the declaration of the function.

l In C , use const to declare a variable, means that the variable is a type of constant, which can replace #define, and more than one type information than #define, and it executes the link, can be placed in the header file; But in C, its declaration must be placed in the source file (ie .c file), constraint declare a variable in C in C, in addition to can't change its value, it is still a variable, such as

Const int maxaRray = 255;

Char store_char [maxArray]; // C is legal, C is not legal

l Contention pointer is paying special attention to. example:

Char * const APTR = mybuf; // constant pointer

* APTR = 'a'; // Legal

Aptr = Yourbuf; // error

const char * bptr = mybuf; // (pointer BPTR) points to constant data

* BPTR = 'a'; // Error

Bptr = Yourbuf; // Legal

l Constly modified member functions cannot be used for constructors and destructive functions.

转载请注明原文地址:https://www.9cbs.com/read-26337.html

New Post(0)