Use of const in c ++
In c ++, const is such a thing: if you want something that someone else cannot modify, it will take effect.
The usage of const is as follows:
A. Modify Constants
Const int;
Int const;
No matter where the const is placed, the effect is actually the same.
B. Modify the pointer:
Const double * p -- before the const pointer *, it indicates that the memory pointed to by the current pointer cannot be changed.
Double * const p -- after the const is placed on the pointer *, it indicates that the current pointer cannot be changed.
Const double * const p -- the current pointer and the memory pointed to by the pointer cannot be changed.
C. Modify parameters
Void funcA (const TYPE Value) {}-- const modifier parameter, indicating that this parameter cannot be changed in the function body.
D. Modify the function return value
Const int fun1 () {} -- const modifies the return value of the function. Therefore, constint result = fun1 () is required during the call, which indicates that the return value cannot be changed.
E. Modify member functions
Class
{
Void func () const;
// Const modifies A member function, which means that the function cannot modify the member variables in Class A. For example, you just want to get A value in Class A instead of changing it, it is helpful for program robustness.
}
If the blog description is incorrect or inaccurate, please point it out! Thank you!