Const defines a variable that is not allowed to be changed to produce a static effect. Using const can improve the security and reliability of the program to some extent. One, const modifier variable
const int n=5;
int const n=5;
These two kinds of writing is the same, is to indicate that the value of the variable n can not be changed, it should be noted that the use of const modifier variable, must be initialized to the face, otherwise it can no longer be assigned.
Const char* str= "FDSAFDSA";
Without a const modifier, we might write a statement str[4]= ' X ' in the back, which would result in an assignment to the read-only memory area, and then the program would terminate abnormally immediately. With the const, this error can be checked out as soon as the program is compiled, which is the benefit of Const. Let logical errors be found at compile time. two, the const and the pointer
int b = 500;
Const int* A = &b; [1]
int const *A = &b; [2]
int* const A = &b; [3]
Const int* Const A = &b; [4]
If the const is on the left side of the asterisk, the const is used to modify the variable that the pointer points to, that is, the pointer is a constant, and if the const is to the right of the asterisk, the const is the cosmetic pointer itself, i.e. the pointer itself is a constant.
Therefore, the same is the case for [1] and [2], where the pointer points to a constant (which is independent of the position of the variable declaration), which does not allow changes to the content, such as *a = 3;
[3] For the pointer itself is a constant, and the pointer is not a constant, in which case the pointer itself cannot be changed, such as a++ is wrong. However, the values stored in the address can be changed, and can be modified by other pointers to the change of address.
int a=5;
int *p=&a;
int* Const n=&a;
*p=8;
[4] is constant for both the pointer itself and the content pointing to it. three, const modifier parameters
void Fun (const a a);
It is not possible to change the parameters that are passed in, which enhances the correctness and reliability of the program.
void Fun (const A * A);
You cannot change the contents of a pointer that is passed in, protecting what the original pointer points to.
void Fun (const a& A);
The referenced object passed in cannot be changed to protect the properties of the original object.
Note: Parameter const is usually used for parameters as pointers or references; Four, the const modified return value
If you modify the function return value with the "pointer Pass" method, the contents of the function return value (that is, the pointer) cannot be modified, and the return value can only be assigned to the same type of pointer with the const modifier.
such as functions
const char * GetString (void);
The following statement will present a compilation error:
Char *str = GetString ();
The correct usage is
const char *STR = GetString (); Five, const modifier function
void fun () const;
If a member function does not modify the data member, it is best to declare it as const, because the data member is not allowed to be modified in the const member function, and if modified, the compiler will make an error, which greatly improves the robustness of the program. vi. comparison with #define
The purpose of the const rollout is to replace precompiled directives (such as define), to eliminate its drawbacks, and to inherit its benefits. (1) can save space and avoid unnecessary memory allocation. For example:
#define PI 3.14159//Chang
const double pi=3.14159; Pi is not placed in RAM at this time
Double I=pi; The pi allocates memory at this time and is no longer assigned.
Double I=pi; Macro substitution during compilation, allocating memory
Double J=pi; No memory allocation
Double J=pi; Then make a macro substitution and allocate memory again.
The const definition constants from the assembly point of view, just give the corresponding memory address, rather than #define as given is the immediate number, so the const defined constants in the process of running the program only one copy, while the #define defined constants in memory have several copies. (2) Improve efficiency.
Instead of allocating storage space for ordinary const constants, the compiler saves them in a symbol table, which makes it a constant during compilation, without the operation of storing and reading memory, making it highly efficient.
Content reference to: https://blog.csdn.net/arduousbonze/article/details/1609833
https://blog.csdn.net/xingjiarong/article/details/47282255