Static is a very common modifier in C , which is used to control the storage methods and visibility of the variable. Here I will talk about the fact that the STATIC modifier is fully analyzed from the Static modifier. Two major roles of Static:
First, control storage mode:
Static is introduced to inform the compiler, store the variable in the static storage area of the program and the stack space.
1. Terminal: The variable defined inside the function is executed to its definition at the program, the compiler allocates space on the stack. Let's know that the space allocated on the stack is released at the end of this function. This creates a problem: How can I achieve if I want to save the value of this variable in the function to the next call? The most easy way to define a global variable, but there are many shortcomings defined as a global variable, the most obvious disadvantage is to destroy the access range of this variable (so that variables defined in this function are not only controlled by this function ). 2, Solution: STATIC is introduced in C , use it to modify the variable, it can indicate that the compiler saves this variable in the static storage area of the program, so that the purpose is achieved, and the access range of this variable constant.
Second, control visibility and connection type:
Static has a role that limits the visible range of the variable in the compilation unit, making it an internal connection, at this time, its antonym is "extern".
STITIEC Summary: Static always makes the variable or object storage form into static storage, the connection mode becomes an internal connection, for local variables (already internal connection), it only changes its storage method; for global variables (already It is static stored), which only changes its connection type.
STATIC member in the class:
I. Reasons and role:
1. You need to interact between individual objects of a class, that is, a data object is required for the entire class rather than an object service.
2. At the same time, it is necessary to do not destroy the encapsulation, that is, this member is required to hide the interior of the class and is not visible to the outside.
The STATIC member of the class satisfies the above requirements because it has the following characteristics: there are independent storage area belongs to the entire class.
Second, pay attention:
1. For static data members, the connector guarantees that it has a single external definition. Static data members are initialized in order by definition, pay attention to static members nested, to ensure that the nested members have been initialized. The order of elimination is the reverse order of initialization.
2, the static member function of the class is an object that belongs to the entire class, so it doesn't have this pointer, which results in only the static data and static member functions of the class.
Const is a type modifier that is common in C , but I found it in my work, many people use it just want to be obvious, so, sometimes it will be used, but in some subtle occasions, it is not so lucky. In fact, most of them are due to this source. Therefore, in this article I will analyze const. Trace your own source, the essence, I hope to understand the const by everyone, according to the relationship of thinking, divided into the following sections.
Why is C introduced Const
C proposals originally introduced (or retained) const key based on what kind of purpose? This is an interesting and beneficial topic that is very helpful for understanding const.
1. Everyone knows that C has a type of strict compilation system, which makes the error of the C program to find a lot in the compilation phase, so that the error rate is greatly reduced, so it has become C with C, there is a prominent advantage aspect. 2. The common preprocessing command #define variablename variableValue can be easily replaced, and this value is replacing at least three advantages:
One is to avoid the fuzzy number of significance, making the program language clear, as in the following example: #define user_num_max 107 This avoids the confusion brought directly by using 107.
Second, the adjustment and modification of parameters can be easily performed, as in the case, when the number of people is changed to 201, the change will change here,
The third is to improve the performance efficiency of the program. Since the pre-built translator is used, it is not necessary to assign a storage space for these constants, so the efficiency of the execution is higher. In view of the above advantages, the use of this predefined instruction can be seen everywhere in the program.
3. Speaking here, everyone may confuse the above 1 point, what is the relationship between 2 points with const?, Good, please then look down:
Although there are many advantages over, it has a more deadly disadvantage, that is, the pre-processing statement is only a simple value replacement, lack of type detection mechanism. Such pre-processing statements cannot enjoy the benefits of C strict type checks, which may become hidden dangers that trigger a series of errors.
4. Ok, the first phase conclusion: Conclusion: The initial purpose of Const introduced is to replace the pre-compiled instructions, eliminate its shortcomings while inheriting its advantages. Now it has become:
Const Dattype VariablenaMe = variableValue;
Why can const can replace a predefined statement? What is the big god of CONST, so that it can vibrate a predefined statement?
1. First, with constant values, there is variability, which is the basis of replacing the predefined statement.
2. Second, it is obvious that it can also avoid the fuzzy numbers, which can also be easily adjusted and modified.
3. Third, C compilers typically allocate storage space for ordinary constants, but save them in the symbol table, which makes it a constant during compilation, no operation of the memory and read memory, make it efficiency Also very high, this is also an important foundation for replacing the predefined statement.
Here, I have to mention, why is this that it is also the basis of it can replace the predefined statement, because the compiler does not read the stored content, if the compiler is allocated to the storage space, it is not possible Become a constant during compilation.
4. Finally, Const definitions are like a normal variable definition, which will be detected by the compiler to it, eliminating the hidden dangers of the predefined statement.
CONST usage classification details
1. Const * a; file: // a variable, * a non-variable int * const A; file: // a is not changeable, * a variable
Analysis: const is a type modifier that combines the left, which is a type modifier on the left and is a type modifier, so INT const is limited * a, not limited A. INT * constly defines A, does not limit * a. 2. Contribution value parameters of Const Limit function:
Void Fun (const Int var);
Analysis: The above-described write method defined parameters cannot be changed in the function body. It is understood that the change in VAR in the function body does not affect the external function. Therefore, this definition is not related to the user's user, only related to the writer of the function.
Conclusion: It is best to limit the internal number of functions, and shield the external calorie to avoid confusion. If you can rewrite as follows:
Void Fun (int var) {const INT & VARALIAS = var;
VARALIAS ....
.....
}
3. Constitu fair value return value:
Const int fun1 ();
Const myclass fun2 ();
Analysis: The return value of the above-mentioned write method cannot be updated. When the function returns the type of internal (such as FUN1), it is already a value, of course, cannot be assigned, so, at this time constant, it is best to remove, so as not to confuse . When a function returns a custom type (such as FUN2), this type still contains a member of the variable that can be assigned, so it makes sense.
4. Transfer and return address: This situation is most common, and the characteristics of the address variable are identical, and the meaning is used in appropriate use of constity.
5. Const Limit class member function:
Class classname {
PUBLIC:
INT fun () const;
.....
}
Note: The form of such const is a regulation, and it is also not confusing. CONST is used in the declaration of this function, because const has become part of the type information.
Get the ability: You can operate a constant object.
Lost ability: You cannot modify the data member of the class, you can't call other functions that are not consts in the function.
In this article, Const knowledge I have not speaking, because I don't want to turn it into a C textbook. I just want to explain it in detail. I will try to say very detailed, because I hope that I have some ideas in a very relaxed and casual atmosphere. After all, the programming is also relaxed, and a part of the happy life . Sometimes, you will be amazed that the world is so beautiful.
After talking about constings, this article is again on this keyword, which puts this article in this position because the introduction of the keyword inline is very similar, and below is divided into the following sections. Equalize.
Causes of Inline keywords in C :
The Inline keyword is used to define a class of inline functions, introducing its main reason is to use it to replace the macro definition in the expression in C.
An example of a macro definition in expression:
#define expressionname (var1, var2) * (var1-var2)
Why replace this form, and listen to me:
1. First, talk about the reason for using this form macro in C, C language is a high efficiency language, which is defined in the form and uses a function like a function, but it uses the preprocessor implementation, no parameter pressure A series of operations such as stack, code generation, therefore, the efficiency is high, this is a major reason it is used in C.
2. This macro definition is similar to a function in the form, but when using it, it is just a simple replacement in the pre-processor symbol table, so it cannot be detected by parameter validity, and cannot enjoy the C compiler strict type. The benefits of checking, and its return value cannot be enforced to convert to a suitable type of convertible, so that its use has a series of hidden dangers and limitations. 3. Introducing access control of classes and classes in C , so that if an operation or an expression involves a category protection member or private member, you cannot use this macro definition (because you can't put this pointer. In a suitable location).
4. The purpose of Inline is also to replace this expression of the macro, which eliminates its shortcomings, and inherits its advantages well.
Why is INLINE to replace the form of formula formula?
Corresponding to 1-3 points above, as follows:
1. Inline defined class's inline function, the code of the function is placed in the symbol table, directly replacing it directly, (like macro is unavoidable), there is no call overhead, the efficiency is also very high.
2. Obviously, the inline function of the class is also a real function. When calling an inline function, the compiler will first check the type of the parameters, and ensure that the call is correct. Then make a series of related inspections, just like any of the true functions. This eliminates its hidden danger and limitations.
3. Inline can be used as a member of a class, of course, can use the protection member and private member of the class.
When I use the inline function:
First, you can use the inline function to completely replace the macro definition of the form of the form.
Also note that the inline function will only be used very simple in the function content, because the code of the inline function will be expanded in any place to call it. If the function is too complex, the evil consequences of code expansion is likely to The benefits that will be greater than the improvement of efficiency. The most important use place for inline functions is the access function for class.
How to use classes of the inline function:
Simply mention the use of inline:
1. Define this function in the class:
Class classname {
.....
....
GetWidth () {Return M_LPICWIDTH;}; // If you are directly defined in the class, you may not use inline modification ....
....
}
2. Declare in the class, definition in the class:
Class classname {
.....
....
GetWidth (); // If you are directly defined in the class, you can do not use inline to modify
....
....
}
Inline getWidth () {
Return m_lpicwidth;
}
In this article, talk about a special function, class's Inline function, its source and characteristics are very similar to const in some point, can look up with Const. In addition, there have been many friends to communicate with me Mail, giving me a lot of questions, giving me a lot of inspiration, thank you here.