The const keyword is used to denote a variable or function, or another type of data whose value or its own value is prohibited from being modified.
However, it is important to note that even if the variable is modified to a const type, its nature is still a variable, not a constant.
Perhaps you would say, what difference does it make? I don't feel the difference in the code. In fact, the difference between them is compiler compilation. For the compiler, a variable means allocating memory space, whereas a constant is just a single card integer that is recorded in the compiler's memory table during compilation. The value of the variable must be initialized, unless it is decorated with extern, which tells the compiler that the variable is a global const variable, where the compiler does not know for a moment, so allow it to define its value elsewhere, and once it is defined, it is forbidden to modify it again.
In the case of more complex cases, the const modifier variable is unexpected:
1. char * const x = "Diamond"; X is forbidden to modify, but * (x) can be modified: *x = "Kongdom"; OK | x + +; ERROR!!!
2. const char *s = "Diamond"; * (s) prohibit modification, that is, the string is forbidden to be modified: *s = "Kingdom"; ERROR!!! | s++; Ok
3. Person p_01;
Const person *P = &P_01; object is const
person Const *P = &P_01; object is const
Person *const p = &P_01; The pointer is a const
Const person *const p = &P_01; Both the object and the pointer are const
So what exactly is the recommended use of const?
1. Modifier function Parameters:
We all know that in C + + 's function arguments, there are three forms of value passing, reference passing, and pointer passing. When the two parameters are passed, when the function is called, the value of the argument is modified by the action inside the function, so if you do not need to modify it, is it not possible to move the reference and the pointer? The answer, of course, is no, and at this point you simply use the const in the function argument list to modify the variable. For example:
void func_01 (const int *x); Here the compiler will disallow changes to the value in the memory space that the pointer points to in the input function.
int a = 100;
Func (&a); Put a pressure stack
const int b = A; Put the value of a in B
Func (&B); Put B pressure Stack
b = a++//error! The value of B is forbidden to be modified
void func_02 (const int i)
{
i++; error! The value of I is forbidden to be modified
}
2. Modify Member Letters
This means that this is a const
int get_age () const;
int student::set_age (int age)
{
This.age = age;
}
int Student::get_age () const
{
this.age++; error! Disallow modification of member variables
Set_age (16); error! Prohibit calling a member without cosnt adornments
return age; Ok
}
3. Objects that decorate a class
The value inside the object is forbidden to modify
4. About function overloading
Set: A class student has the following member functions:
void Func ();
void func () const;
And: In the main () function, there are:
Const Student Stu;
Stu.func ();
Then: The above two functions constitute function overloads, which can implement static polymorphism.
You might ask that these two functions should be duplicated and should be wrong. They cannot form function overloads. But that's not really the case.
Here are the two func () functions, all of which belong to the class's member functions, and they are actually like this:
void func (Student *this);
void func (const Student *this);
That is, the modifier of the second func () function is const, which is used to decorate the object of the class itself, that is, this
When an object of type const is Stu, the Func () function is called, and the Func () const function of the same const type is called.
Use of the const keyword in C + +