semantics of the const keyword
Simply put, the const keyword will decorate a quantity as "constant", that is, the amount of non-volatile. This amount can be a basic type variable, a class object, a pointer, an object reference, a class member function in C + +.
Const Variable
The C++ standard stipulates that the Const keyword is placed before the type or variable name equivalent:
Const int 5; <==> int const 5; const intNULL; <==> int constNULL;
Bjarne A mnemonic method in the C + + programming Language book: to read a const declaration from right to left, * to read pointer to:
Char*ConstP// p is a const pointer to charconst char * p;// P is a pointer to const char Char* * p;// P is a pointer to pointer to charconst char * * p;// P is a pointer to pointer to const char Char*Const* p;// P is a pointer to const pointer to charconst char *Const* p;// P is a pointer to const pointer to const char Char**ConstP// p is a const pointer to pointer to charconst char **ConstP// p is a const pointer to pointer to const char Char*Const*ConstP// p is a const pointer to const pointer to charconst char *Const*ConstP//P is a const pointer to const pointer to const char
Const member Functions
The const member function of the class indicates that the member function has read-only permission on the class member and does not have write permission. The objects of a class are divided into constant and extraordinary objects, and the member functions of a class are also divided into constant functions and very much functions. The mutual invocation relationship between the two is as follows:
•A constant function can be called by a very mass object;
•A very mass object can call a very mass function;
•A constant object cannot call a non-const function;
•A constant object can call a constant function;
[c++.gleaning] const keyword