The role of static in C + +

Source: Internet
Author: User

There are three main functions of static:

(1) Local static variables

(2) External static variables/functions

(3) static data member/member function

The first two C and C + + have, the third is only in C + +, the following are described below:

First, local static variables

Local variables can be divided into three kinds of auto, static and register according to the storage form. Where register is not commonly used, the following mainly talk about the difference between auto and static.

    1. Storage space allocation and life cycle are different

The auto type local variable is a normal local variable (the non-decorated local variable defaults to that type). This type of local variable is stored on the stack, and in dynamic storage, the life cycle is limited to the function that defines it, and the function ends and it is automatically freed. Static type local variables are stored in the quiescent store and are not freed during the entire run of the program. The scope of the two is the same, but the lifetime is different.

    1. The static local variable is initialized at the first run of the module and operates only once.

    2. For local static variables, if the initial value is not assigned, the compilation period automatically assigns the initial value of 0 or NULL, and the initial value of the auto type is indeterminate. (For class object exceptions in C + +, an object instance of class will automatically call the default constructor if it is not initialized, regardless of whether it is a static type)

Features: "Memory" of static local variables and "global" of Lifetime

Second, external static variables/functions

Static in C has a second meaning: a global variable and function that is used to represent a file that cannot be accessed by other files. However, in order to limit the scope of global variables/functions, a function or variable is static before it is added to the function. But the meaning of "static" here is not the means of storage, but the scope of the function is confined to this file (so called intrinsic function). Note At this point, for an external (global) variable, whether or not there is a static limit, its storage area is in the static storage area, and the lifetime is global. At this point, static is only a function of the domain restriction, scoped within the module (file).

The advantage of using intrinsic functions is that different people write different functions without worrying about the functions they define and whether they have the same name as the functions in other files.

Third, static data member/member function (c + + specific)

c+ + reuses this keyword and gives it a different third meaning from the previous one: the variables and functions that represent any particular object belonging to a class other than this class. This is the biggest difference from the normal member function,
is also where the application is, such as when counting the objects of a class, how many instances of the class are generated by the count,
You can use a static data member. In this case, static is neither scoped nor extended lifetime, but rather indicates the uniqueness of the variable/function in this class. This is also the meaning of "variables and functions that belong to a class and not to any particular object of this class." Because it is unique to the entire class, it is not possible to belong to an instance object. (for static data members, the member function, regardless of whether it is static or not, has only one copy in memory, and when the normal member function is called, it needs to pass in the this pointer, when the static member function is called, there is no this pointer.)

Static is the key word in c,c++. But the use of the two is somewhat different:
One, for the use of process static:
1, static global variable. /static function;
1) The scope of global variables in C language is the whole program; As long as you are in other files (if you want to use a global variable), you can use the global variables already defined in other files with the extern declaration!
One consequence of this is that the program is more difficult to maintain, and the naming error is more subtle!
In order to limit its scope in other files.

Static global variables are often used. is to precede the global variable with the static keyword. The area of the variable's data allocation is still within the global zone, but his scope is only in the current file, regardless of whether the other file declares an external variable!
2) static function;
Like a static global variable, the original global function has no access to the program domain! can only be accessed within the file domain!

2, static local variables;
For C or a variable inside a function within C + +, by default it is a temporary local variable that is allocated above the stack. The variable is destroyed after the function call is finished.
To be able to share in other functions, you can use static local variables.
Add static before the variable. At this point the scope of the variable is still inside the function but his life cycle is global!

Second, object-oriented static members;
The member declared as static belongs to the class. Because he is loaded with the initialization of the class and will only be loaded once!
The static member function, regardless of whether it is static or not, has only one copy in memory, and when the normal member function is called, it needs to pass in the this pointer, and when the static member function is called, there is no this pointer;
Therefore, you should pay attention to it when you define it. It is not necessary to declare the same as virtual in the CPP when implementing static Statics functions!

The role of static in C + +

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