(The following is a reference from the Internet content. Thanks here, the author expressed gratitude! I know that I am dangerous. Reading the following text will have a comprehensive understanding of CONST in C . Const is a type modifier common in C , with some subtle applications, if you don't make this source, the error is inevitable. The const will be analyzed in this article. 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 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 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; // a variable, * a variable INT * const same; // a variable, * 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 this constant is a regulation because the location of the Const modification function has been occupied, and it is not allowed to take such a way. 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.