C and C++ static const inline three keywords detailed summary

  • 2020-04-02 01:38:21
  • OfStack

One, about static
Static is a very common modifier in C++, which is used to control the storage and visibility of variables. Next, I will start from the causes and effects of static modifiers, and comprehensively analyze the essence of static modifiers.

Static has two main effects:

One, control storage mode
Static is introduced to tell the compiler to store variables in the program's static store instead of on the stack.

Reasons:
Function within the definition of the variables in the program execution to its definition, the compiler space allocated on the stack for it, as we all know, function of the space allocated on the stack will release at the end of this function, the result is a question: if you want to be the function of the value of this variable is saved to the next call, how to do?

The easiest way to think about it is to define a global variable, but defining it as a global variable has many disadvantages, the most obvious of which is that it breaks the scope of access to the variable (making the variables defined in the function more than controlled by the function).

Solutions:
Therefore, in C++, static is introduced to modify variables, which can instruct the compiler to save the variable in the program's static storage allocation space, thus achieving the purpose while keeping the variable's access scope unchanged.

Control visibility and connection types  
Static also has the effect of limiting the visibility of a variable to the compilation unit, making it an internal connection, where its opposite is "extern."

Static analysis and summary:
Static always makes the storage form of variable or object become static storage and the connection mode become internal connection. For local variables (which are already internally connected), it only changes the storage mode. For global variables (which are already statically stored), it only changes its connection type.

A static member of a class
Causes and effects:

1. You need to interact among the objects of a class, that is, you need a data object to serve the whole class rather than an object.

2. At the same time, it tries not to break the encapsulation of the class, that is, it requires this member to be hidden inside the class and invisible to the outside world.

The static member of the class fulfills this requirement because it has the following characteristics: a separate storage area, belonging to the entire class.

Note:

1. For a static data member, the connector ensures that it has a single external definition. Static data members are initialized in the order in which they are defined. Note that when static members are nested, the nested members are initialized. The order of elimination is the reverse order of initialization.

2. The static member function of a class is an object belonging to the whole class rather than the class, so it does not have this pointer, which causes it to only access the static data and static member function of the class.

Ii. About const
Const is a common type modifier in C++, but in my work I've found that many people use it for granted, sometimes correctly, but in subtle situations, not so lucky, mostly because they don't understand the source. Here I will differentiate the const. Looking back to its origin and exploring its essence, I hope it can be helpful for everyone to understand const. According to the thinking of the connection, it is divided into the following parts to elaborate.

Why did C++ introduce const

For what purpose did the C++ initiators introduce (or retain) the const keyword? This is an interesting and useful topic for understanding const.

1. As we all know, C++ has a strict compilation system, which makes the errors of C++ programs can be found in the compilation stage, so that the error rate is greatly reduced, therefore, it has become C++ compared with C, has a prominent advantage.

2. The preprocessing instruction #define VariableName VariableValue, which is very common in C, can be easily substituted for values.

This value substitution has advantages in at least three aspects:

One is to avoid the occurrence of fuzzy Numbers, so that the program semantics are smooth and clear, as shown in the following example:
#define USER_NUM_MAX 107 this avoids the confusion of using 107 directly.

Second, it can be very convenient to adjust and modify the parameters, as shown in the above example, when the number of people from 107 to 201, can be changed here;

The third is to improve the execution efficiency of the program, because the use of the precompiler for value substitution, there is no need to allocate storage space for these constants, so the execution efficiency is higher.

Given the above advantages, the use of such predefined instructions is ubiquitous in programs.

3. At this point, you might wonder what the 1 or 2 points above have to do with const. Ok, then look down:

Although preprocessing statement has many advantages above, but it has a fatal disadvantage, that is, preprocessing statement is only a simple value replacement, the lack of type detection mechanism. This prevents preprocessed statements from taking advantage of C++ 's strict type checking and can lead to a series of errors.

4. Okay, here's the first step:

Conclusion: the original purpose of const is to replace precompiled instruction, eliminate its disadvantages and inherit its advantages.

Now it takes the form of:

Const DataType VariableName = VariableValue;

Why is const a good replacement for predefined statements?

What's the magic of const that allows it to replace predefined statements with a wave?

First of all, A constant value decorated with const that is immutable, which is the basis for it to replace a predefined statement.

Second, Obviously, it can also avoid the fuzzy meaning of the number, can also be very convenient to adjust and modify the parameters.

Third, C++ compilers typically do not allocate storage space for ordinary const constants, but instead store them in symbol tables, which makes it a constant at compile time and makes it efficient without having to store and read memory, which is also an important basis for it to replace predefined statements.

Here, I'll mention why this is the basis for it to replace the predefined statements, because the compiler doesn't read the stored content, and if the compiler allocates storage space for const, it can't be a constant at compile time.

Finally, the const definition, like a normal variable definition, is type-detected by the compiler, eliminating the pitfalls of predefined statements.

A detailed analysis of the usage of const
1. Const is used for pointer analysis in two cases
Int const * A; file://A variable, *A immutable
Const int * A; file://A immutable, *A variable

Analysis: const is A left - bound type modifier, it and its left - bound type modifier are A type modifier, so int const qualifies *A, does not qualify A. Int *const qualifies A, does not qualify *A.

2. Parameter of the passed value of const qualifier
Void Fun (const int Var);
Analysis: the above written limit parameter cannot be changed in the function body. According to the characteristics of value transfer, the change of Var in the function body will not affect the function outside. Therefore, this qualification is not relevant to the user of the function, but only to the writer of the function.

Conclusion: it is best to qualify inside the function and mask external callers to avoid confusion. If it can be rewritten as follows:


void Fun(int Var) 
{ 
const int & VarAlias = Var; 
VarAlias .... 
..... 
} 

3. Return value of value type of const qualifier
Const int Fun1 ();
Const MyClass Fun2 ();

Analysis: the above method qualifies the return value of the function cannot be updated, when the function returns the internal type (such as Fun1), is already a value, of course, can not be assigned to update, so, at this time const meaningless, it is best to remove, in order to avoid confusion. When a function returns a custom type (such as Fun2), that type still contains variable members that can be assigned, so it makes sense.

4. Transfer and return addresses
This is the most common case, from the characteristics of address variables, appropriate use of const, the meaning is clear.

5. Const qualifies member functions of a class


class ClassName { 
public: 
int Fun() const; 
   ..... 
} 

Note: this form of post-const is a requirement and is not intended to cause confusion. Const is used in both the declaration and the definition of this function because const is already part of the type information.

Ability to manipulate constant objects.

Incapacity: cannot modify the data members of a class, cannot call other non-const functions in a function.

Iii. Inline
After talking about const above, I will talk about the keyword "inline" next. The reason why I put "inline" in this position is that the reason why the keyword "inline" is introduced is very similar to const. The following is divided into the following parts.

The reason for the inline keyword in C++ :
The inline keyword is used to define an inline function of a class, introduced primarily as an alternative to the macro definition in the form of an expression in C.


Example of a macro definition in the form of an expression:
# define ExpressionName (Var1, Var2) (Var1 + Var2) * (Var1, Var2)

Why do you want to replace this form? Let me explain:

1. First talk in C use this form the reason of the macro definition, C language is the language of a high efficiency, the macro definition in form and use as a function, but it is implemented using the preprocessor, without the pressure parameter stack, code generation and so on a series of operations, as a result, the efficiency is very high, which it is used in C one of the main reasons.

2. The macro definition is similar to a function in form, but in the use of it, just do the preprocessor symbol table of the simple replacement, so it can't to the effectiveness of the testing parameters, also will not be able to enjoy the benefits of the c + + compiler strict type checking, and it also cannot be cast to the return value of a convertible, suitable types of such, it is the use of a series of hidden trouble and limitations.

3. C++ introduced access control for classes and classes, so that if an operation or an expression involves a protected or private member of a class, you can't use this macro definition (because you can't put this pointer in place).

4. Inline is also designed to replace this macro definition in the form of an expression, eliminating its disadvantages while inheriting its advantages.

Why is inline a good alternative to the predefined form of expressions?

Corresponding to the 1-3 points above, it is expounded as follows:

1. Inline function of the inline defined class, the code of the function is put into the symbol table, when used directly to replace, (like a macro expansion), no call overhead, and very efficient.

2. Obviously, the inline function of a class is also a real function, and when the compiler calls an inline function, it first checks the type of its argument to make sure the call is correct. It then runs a series of correlation checks, just like any real function. This eliminates its hidden dangers and limitations.

3. Inline can be used as a member function of a class, where, of course, the protected and private members of the class can be used.

When to use the inline function
First, you can use the inline function to completely replace the macro definition in the form of an expression.

Note also that inline functions are generally only used when the content of a function is very simple, because the code for an inline function will expand wherever it is called, and if the function is too complex, the bad effects of code bloat are likely to outweigh the benefits of increased efficiency. The most important use of inline functions is the access function for the class.

How to use the inline function of a class

Simply mention the use of inline:

1. Define this function in a class


class ClassName{ 
..... 
.... 
GetWidth(){return m_lPicWidth;}; //If you define it directly in a class, you can do without the inline modification
.... 
.... 
} 

2. Declare in the class and define outside the class:

class ClassName{ 
..... 
.... 
GetWidth(); //If you define it directly in a class, you can do without the inline modification
.... 
.... 
} 
inline ClassName::GetWidth() 
{ 
return m_lPicWidth; 
}

In this article, we talk about a special function, the inline function of the class, its origin and characteristics in a way similar to const, can be seen together with const.


Related articles: