Common modifier static in C ++

Source: Internet
Author: User

 

Static is a common modifier in c ++. It is used to control the storage and visibility of variables. Next I will talk about the reason and role of static modifiers, the essence of the static modifier is comprehensively analyzed.

Understand the static keyword in C ++

Static and extern keywords in C/C ++

Summary of static usage in C ++

Static:

I. Control Storage Methods:

Static is introduced to inform the compiler that the variables are stored in the static storage area of the program rather than the stack space.

1. Cause

When a variable defined in a function is executed to its definition, the compiler allocates space for it on the stack, the space allocated by the function on the stack is released at the end of the function execution. This creates a problem: If you want to save the value of this variable in the function to the next call, how to implement it?

The easiest way to think of is to define a global variable, but defining a global variable has many disadvantages, the most obvious drawback is that the access range of the variable is broken (so that the variables defined in this function are not controlled by this function ).

2. Solutions

Therefore, static is introduced in c ++ to modify the variable. It can instruct the compiler to save the variable in the static storage area of the program, the variable's access range remains unchanged.

Ii. control visibility and connection type:

Static also has a function, which limits the visible scope of the variable to the compilation unit and makes it an internal connection. In this case, its antonym is "extern ".

Static function analysis summary:

Static always changes the storage form of variables or objects to static storage, and the connection mode to internal connections. For local variables (which are already internal connections), it only changes the storage mode; for a global variable (which is already in static storage), it only changes its connection type.

Static member in the class:

I. Causes and functions:

1. Interaction between objects in a class is required, that is, a data object must serve the entire class rather than a certain object.

2. At the same time, we strive not to undermine the encapsulation of the class, that is, this member is required to be hidden inside the class and invisible to the outside world.

The static member of the class meets the preceding requirements because it has the following features: it has an independent storage zone and belongs to the entire class.

2. Note:

1. for static data members, the connector ensures that it has a single external definition. Static data members are initialized sequentially according to the sequence in which they appear. Note that when static members are nested, make sure that the nested members have already been initialized. The order of cancellation is the reverse order of initialization.

2. The static member function of the class belongs to the entire class rather than the class object, so it does not have the this pointer, which leads to the ability to only compile static data and static member functions of the class.

We hope that the above content will help you.

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.