C ++ Analysis Series

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.

 

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 the program executes the variable defined in the function 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. Solution: Therefore, static is introduced in C ++ to modify the variable, which can instruct the compiler to save the variable in the static storage area of the program, this achieves the goal and keeps the access range of this variable unchanged.

 

Ii. control visibility and connection types:

 

  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 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 (internal connections already exist ), it only changes the storage mode. For global variables (which are already in static storage), it only changes the connection type.

 

ClassStatic member:

 

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.

 

ClassStatic members meet the preceding requirements because they have the following features: they have independent storage areas and belong 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.

 

Const is a type modifier commonly used in C ++, But I found at work that many people only want

 

RanerAnd sometimes use the correct method, but in some subtle occasions, it is not so lucky.

 

Mostly because the source is not clear. In this articleConst. Trace its source, investigate its essence,

 

Hope to understandConst is helpful. It is divided into the following parts based on the undertaking relationship of thinking:

 

.

 

Why is const introduced in C ++?

 

  What is the purpose of the C ++ initiator to introduce (or retain) the const keyword ?, This

 

Is an interesting and useful topic for understandingConst is very helpful.

 

1. As you know, C ++ has a strict type compilation system, which makes the errors of C ++ programs in the compilation stage

 

Many can be found, which greatly reduces the error rate.C ++ has outstanding advantages over C

 

.

 

2. Common Pre-processing commands in C # define VariableName VariableValue can be easily used for Value Replacement

 

This value substitution has at least three advantages:

 

The first is to avoid the appearance of numbers with vague meanings, so that the program semantics is fluent and clear, as shown in the following example:

  # Define USER_NUM_MAX 107 avoids the confusion caused by using 107 directly.

 

Second, you can easily adjust and modify parameters. In the preceding exampleWhen 107 is changed to 201

 

You can change it here,

 

Third, it improves the execution efficiency of the program. Because the pre-compiler is used for value substitution

 

Constant distribution of storage space, so the execution efficiency is high.

 

In view of the above advantages, the use of such predefined commands is everywhere in the program.

 

3. Speaking of this, you may be confused about the relationship between the first and second points and const ?, OK. Proceed to the next step.

 

It seems that:

 

A pre-processing statement has many advantages, but it has a fatal disadvantage, that is, a pre-processing statement.

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.