C analysis series talks (2) Zheng Liqun
Const is a type modifier common in C , but I found it in my work, many people use it just want to be
In this way, sometimes it will be used, but in some subtle occasions, it is not so lucky, and the substance is originally
Most because they did not make this source. Therefore, in this article I will analyze const. Trace your own source, the essence,
I hope that I can understand that const is helpful, according to the relationship between 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
It is an interesting and useful 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 in the compilation phase.
Many, so that the error rate is greatly reduced, so it has also become a highlight of C and C.
One aspect.
2. The common pretreatment command #define variablename variableValue can be easily replaced
Generation, this value is instead of at least three aspects:
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, it is convenient to adjust and modify parameters, such as above, when the number is changed from 107 to 201,
Change it here,
The third is to improve the performance efficiency of the program. Since the pre-built translator is used, it is not necessary for these
Constant allocates storage space, so the efficiency of 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 and const?, Good, please go down
It seems:
Although there are many advantages over the pretreatment statement, it has a more deadly disadvantage, namely, pre-processing statements
Just just simple value replacement, lack of type detection mechanisms. Such a pre-processed statement cannot enjoy C strict classes
The benefits of type inspection may become hidden dangers that trigger a series of errors.
4. Ok, the first phase conclusions: Conclusion: The initial purpose of Const introduced, is to replace the pre-compiled instruction, eliminate its shortcomings, while inheriting
It advantages.
Now it has become:
Const DataType 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 also avoids the fuzzy number, which can also be carried out.
Adjustment and modification of parameters.
3. Third, C compilers typically allocate storage space for ordinary constants, but save them in characters
The table, which makes it a constant during compilation, there is no operation of storage and reading memory, making it
The efficiency is also very high, and this is also an important basis for replacing the predefined statement. Here, I have to mention it, why is this that it is also the basis of replacing a predefined statement, because
The compiler will not read the stored content, if the compiler is allocated to the storage space, it can't be a
The constant during compilation.
4. Finally, the const definition is like a normal variable definition, it will be checked by the compiler.
Test, eliminating the hidden dangers of predefined statements.
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 of modifier that combines left, it is a type modifier with its left side and is a type.
The modifier, so INT const is limited * a, does not limit 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. According to the feature of the value, VAR is in the letter.
The changes in the plurality will not affect the outside of the function. Therefore, this definition is independent of the user of the function, only with the function
The writer is related. Conclusion: It is best to limit the internal number of functions, and shield the external calorie to avoid confusion. If you can rewrite
under:
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 is not updated. When the function returns the type of internal (such as FUN1)
It is already a value, of course, cannot be assigned, so the conste is meaningless, it is best to remove it, so as not to
confused. When a function returns a custom type (such as FUN2), this type still contains variables that can be assigned
Member, so it makes sense.
4. Pass and return Address: This situation is most common, the characteristics of address variables are known, use const, meaning
Zhao Zhaoran.
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. Sound of this function
CONST should be used in the mid-in-definition, as 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, I don't talk about Const knowledge, because I don't want to turn it into a C textbook.
. I just want to explain its substance and use in detail. I will try it very detailed because I hope it is very light.
Some ideas in a loose atmosphere, after all, programming is also a relaxed, happy life. Have
At the time, you will be amazed that the world is so beautiful.