Const usage summary

xiaoxiao2021-03-06  16

Const ---- Constant Mark, used to define a constant variable, can generate the same role in #define Num 5, but the more advanced role is reflected in the security of the programs, and the compilation optimization. Summarize the use of Const.

1, const Int num = 5; Some variables do not want to be changed after the first value, can be used to sign, when the program is to change these variables, there will be error prompts when compiling.

2, Void Compute (Const & T); Usage of reference parameters can be used to avoid copying some large types of parameters, save memory, but give the call function to change the permissions, can be used when the parameter does not want to be changed Mode of modification. Save space, but also guarantees security.

3, and the four use usage generated by the pointer: A Very quantity of data of the data; B very amount of data constant pointers; C constant data is very powerful pointer; the constant pointer of D constant data. These are actually limited to privileges, for security considerations. Usage is as the name suggests, I explain the constant pointer. The so-called constant pointer is that the content of the pointer data is fixed, that is, the address is fixed. The content of the variable represented by the address does not matter.

4, some usage in the class; can define the const object, that is, the object cannot be changed, the same as the first point. Const member function void compute (int) const ;: This function does not change the properties of the object - value. And the Const object cannot call a non-Const member function, even if the function does not change the value of the object, it will generate compilation errors. These are all considered for security. However, when the member function does not change the value of the object, the recommendation defines the function as a const type, so when the program is trying to change the object, the compiling will prompt the error. And use constly definition of heat, you can optimize compilation and improve performance. Written on the book, the reason I don't know :) It is estimated that the knowledge of the compilation principle is related to the specific design of the compiler.

There are so many things I have learned now ^ _ ^

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

New Post(0)