Static, extern, and volatile usage

Source: Internet
Author: User

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, instead of having one 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 ).

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}

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.

 

From:

Http://unix-cd.com/vc/www/23/2008-07/10299.html

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.