On the series of C + + discrimination

Source: Internet
Author: User

Static is a commonly used modifier in C + +, which is used to control the way variables are stored and visible, and I'll analyze the nature of the static modifier comprehensively from the reason and the effect of the static modifier.

The two main functions of static:

One, control storage mode:

Static is introduced to tell the compiler to store variables in the static store of the program rather than on the stack space.

1. Causes: Variables defined within the function, when the program is executed to its definition, the compiler allocates space for it on the stack, and as you know, the space the function allocates on the stack is released at the end of the function execution, which creates a problem: how does it work if you want to save the value of this variable in a function to the next call?

The easiest way to think about it is to define a global variable, but there are many drawbacks to defining it as a global variable, the most obvious disadvantage being that it destroys the scope of the variable's access (so that the variables defined in this function are not just controlled by this function).

2, the solution: so the C + + introduced a static, with which to modify the variable, it can instruct the compiler to put this variable in the program's static storage space to save, which is to achieve the purpose, and make this variable access scope unchanged.

Second, control visibility and connection type:

Static also has the function of restricting the visible range of a variable to a compilation unit, making it an internal connection, at which point its antonym is "extern".

Static action Analysis Summary: Static always makes a variable or object storage form into static storage, the connection mode into an internal connection, for local variables (already connected to the internal), it only changes its storage mode; For global variables, which are already statically stored, it only changes their connection type.

Static members in a class:

First, the causes and effects:

1, need to interact among the objects of a class, that is, the need for a data object for the entire class rather than an object service.

2, at the same time and strive to not destroy the packaging of the class, that is, the requirements of this member hidden within the class, externally invisible.

The static member of the class satisfies the above requirements because it has the following characteristics: There is a separate storage area, which belongs to the entire class.

Second, note:

1, for static data members, the connector will guarantee that it has a single external definition. Static data members are initialized sequentially in the order in which they appear, and note that when static members are nested, the nested members are initialized. The order of elimination is the inverse order of initialization.

2. A static member function of a class is an object that belongs to an entire class, not a class, so it has no this pointer, which causes it to access only static data and static member functions of the class.

Const is a commonly used type modifier in C + +, but I found in my work that many people use it just for a reason, so that sometimes it will be used right, but in some delicate occasions, it is not so lucky, the essence of the original, mostly because there is no understanding of the source. Therefore, in this article I will be the analysis of the Const. Back to its origin, the essence of the hope that we can understand the const help, according to the thinking of the undertaking relationship, divided into the following sections to elaborate.

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.