Parse static usage in C and C + + and distinguish _c language

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 kinds of C and C + + have, the third is only in C + +, the following are described below:

One, local static variables
In C + +, local variables can be divided into three kinds of auto, static and register according to the storage form. 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 the ordinary local variable (the default for the unmodified local variable is the type). The type local variable is stored on the stack, and in the dynamic store, the lifecycle is limited to the function that defines it, and it is automatically released when the function ends. Static type local variables are stored in a static 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.

2. Static local variables are initialized at the first run of the module, and are operated only once.

3. For local static variables, if the initial value is not assigned, the compile period will automatically assign an initial value of 0 or a blank character, and the initial value of the auto type is indeterminate. (For class object exceptions in C + +, the default constructor is automatically invoked if the object instance of class is not initialized, regardless of whether it is a static type)

Characteristics: "Memory" of static local variables and "global" of life period

Second, external static variables/functions
Static in C has a second meaning: a global variable and function that represents an inability to be accessed by other files. But in order to limit the scope of global variables/functions, a function or variable is statically added before the function becomes a static function. But the meaning of "static" here is not the way of storage, but the scope of the function is limited to this file (so called internal function). Note that at this point, for external (global) variables, regardless of whether there is a static limit, its storage area is in the static storage area, the lifetime is global. The static at this point is only a function of the domain restriction, which limits the scope within this module (file). The advantage of using internal functions is that different people write different functions without worrying about the functions they define and whether they will have the same name as functions in other files.

Third, static data member/member function (c + + specific)
c+ + reuses this keyword and gives it a third meaning that is different from the previous one: a variable and function that represents a class other than any particular object belonging to the class. This is the biggest difference from the ordinary member function, but also its application, for example, when counting the objects of a class, the count generates a number of instances of the class, you can use the static data members. In this case, static is neither a scoped nor an extension lifetime, but rather a indication of 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, whether a member function is static or not, there is only one copy in memory, and when a normal member function is called, the this pointer needs to be passed in, and when the static member function is called, there is no this pointer.)

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.