Usage of const, static, extern, and volatile

Source: Internet
Author: User

1. Const usage:
Why use const?
Code written using symbolic constants is easier to maintain. Pointers are often moved while reading, rather than writing. Many function parameters are read-only and not written. The most common use of const is as the Division label of the array and switch (you can also replace it with an enumerator)

Usage 1: Constant
The macro definition in C must be initialized during declaration. Const limits the usage of constants and does not describe how constants should be allocated. If the compiler knows all the usage of a const, it may not even allocate space for the const. The simplest common case is that the constant value is known during compilation and does not need to be stored. -C ++ program language
Although the variables declared with const increase the allocation space, they can ensure type security.
In the C standard, the constants defined by const are global, and C ++ depends on the declared position.

Usage 2: pointer and constant
When a pointer is used, two objects are involved: the pointer itself and the object referred to by it. Using const "pre-fixed" to declare a pointer will make that object instead of making this pointer a constant. To declare the pointer itself rather than the object to be a constant, you must use the declarative operator * Const.
Therefore, the const that appears before * is part of the basic type:
Char * const CP; // const pointer to Char
Char const * pC1; // pointer to const char
Const char * PC2; // pointer to const char (the last two declarations are equivalent)
Memory reading from right to left:
CP is a const pointer to Char.
PC2 is a pointer to const char.

Usage 3: const modifier function input parameters
Declare the input parameter of the function as const to indicate that this parameter is only for the sake of efficiency, rather than for the call function to modify the object value. Similarly, if the pointer parameter is declared as const, the function will not modify the object referred to by this parameter.
Usually, modify the pointer parameters and reference parameters:
Void fun (const A * In); // modifies pointer input parameters
Void fun (const A & in); // modify the referenced input parameter

Usage 4: Modify the function return value
This prevents users from modifying the return value. The return value must also be a constant or constant pointer.

Usage 5: const modifier member functions
The const object can only access the const member function, but not the const object can access any member function, including the const member function;
Members of the const object cannot be modified, but objects maintained by pointers can indeed be modified;
The const member function cannot modify the data of an object, regardless of whether the object has the const nature. Check whether the member data is modified during compilation.

 

2. Static usage:
Static variables are used in a file. Space is allocated at the beginning of the program, and space is released at the end of the program. The default value is 0, which can be changed during use.
Static variables or static functions can be accessed only by the code in this file. Their names are invisible to other files.
Usage 1: static variables declared inside the function can be used as a communication mechanism between objects.
If a local variable is declared as static, there will be only one static allocation object, which is used to represent this variable in all calls to the function. This object will be initialized only when the execution thread reaches its definition for the first time.
Usage 2: local static object
For a local static object, the constructor is called when the control thread first passes the definition of the object. At the end of the program, the destructor of local static objects will be called one by one in the reverse order they are constructed, without specifying the exact time.
Usage 3: static member and static member functions
If a variable is a part of a class but not a part of each object of the class, it becomes a static member. A static member has only one unique copy, but does not have a copy in each object as a regular non-static member. Similarly, a function that requires a member of the struct class and does not need to be called for a specific object is also called a static member function.
The static member function of a class can only be a static member (variable or function) of a class ).

 

3. Use of extern:
Extern can declare variables defined in other files. In a program, an object can only be defined once. It can have multiple declarations, but the types must be identical. If a variable defined in the global scope or namespace scope is not initialized, it will be initialized by default.
Declare a variable or function as an external link, that is, the variable or function name is visible in other functions. The modified variables (external variables) are statically allocated space, that is, allocated at the beginning of the program and released at the end.
In C ++, you can also specify a link to use another language, which must be used together with a specific conversion character.
Extern "C" declaration statement
Extern "C" {statement block}

 

4. Volatile usage:
Type-modifier, which specifies that an object can be changed by an external process (such as an operating system, hardware, or concurrent process. Volatile is used with variables to allow variables to be accessed and modified by different threads. Declaration Syntax:
Int volatile Vint;
It is often used for memory unit access by asynchronous processes such as interrupt handlers.
In addition to the basic types, the user-defined types can also be modified using the volatile type.
Note: A non-volatile int can be assigned to a volatile int, but a non-volatile object cannot be assigned to a volatile object.
A class with a volatile identifier can only access the subset of its interfaces, and a subset controlled by the class implementer. You can only use const_cast to obtain full access to the type interface. In addition, volatile will pass to its members from the class like Const.

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.