The meaning of const in c
const is a keyword in C language.
const is the abbreviation of constant. Its original meaning is unchanging and not easy to change.
const in C is used to modify built-in type variables, custom objects, member functions, return values, and function parameters.
The const keyword cannot be used together with the static keyword, because the static keyword modifies the static member function. The static member function does not contain this pointer, that is, it cannot be instantiated. The const member function must be specific to a certain instance.
Extended information:
1. Const constants can be defined and are immutable. For example: const int Max=100; Max will generate an error;
2. It facilitates type checking, allows the compiler to have a better understanding of the processing content, and eliminates some hidden dangers. For example: voidf(consstint i) { .........} The compiler will know that i is a constant and is not allowed to be modified;
3. It can avoid the appearance of ambiguous numbers, and the same can be done It is very convenient to adjust and modify parameters. Just like the macro definition, it can be done as long as it doesn’t change, and it will change if it changes!
4. It can protect the modified things, prevent accidental modifications, and enhance the robustness of the program. Still in the above example, if i is modified in the function body, the compiler will report an error; for example: void f(const int i) { i=10;//error! }
5. It can save space. Avoid unnecessary memory allocation.
6. Improved efficiency. Compilers usually do not allocate storage space for ordinary const constants, but save them in the symbol table, which makes it a constant during compilation, without the operation of storing and reading memory, making it very efficient.
Recommended learning: c language video tutorial
The above is the detailed content of What does const mean in c++?. For more information, please follow other related articles on the PHP Chinese website!