C ++ Const

zhaozj2021-02-16  99

Oh, http://www.allaboutprogram.com/post-12558.htm

Why is C introduced in Const C to introduce (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 pre-processing instructions #define variablename variableValue can be easily replaced, this value is replacing at least three advantages: one is to avoid the meaning of the fuzzy number, so that the program is clear, such as 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. As in the above example, when the number of people is changed to 201, it can be changed here, and the third is to improve the execution efficiency of the program, since the pre-compiler is used to replace Do not need to allocate storage space for these constants, 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 look down: the pretreatment statement has many advantages over, but 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 types, 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 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 a predefined statement.

转载请注明原文地址:https://www.9cbs.com/read-11315.html

New Post(0)