Why use const? Code written with symbolic constants is easier to maintain, and pointers are often moved along the edge of the read side, rather than the edge-write edges, and many function parameters are read-only and not written. The most common use of const is as an array of bounds and switch sub-condition labels (also can be substituted with enumerators), categorized as follows:
Constant variable: const type variable name or type const variable name//two equivalent, such as: const int a=5; const a=5 with int; Equivalent
Constant reference: const Type & Reference Name
Constant object: Type Const object Name
Constant member function: Class Name:: Fun (formal parameter) const
Constant group: Type const array name [size]
Constant pointer: const type* pointer name, type* const pointer name
The first hints are: in the constant variable (const type specifier variable name), constant reference (const type specifier & reference name), constant object (class name const object name), const group (type descriptor const array name [size]), and "type specifier" or "Class name" ( In fact, the position of a class name is a custom type specifier can be interchanged. Such as:
First, Const function
This is shown in the following table:
Second, the use of the const
Constant variable: const type descriptor variable name
Constant reference: const type specifier & reference name
Constant object: Class name const object name
Constant member function: Class Name:: Fun (formal parameter) const
Constant group: type specifier const array name [size]
Constant pointer: const type specifier * Pointer name, type specifier * Const pointer name
2.1. Constant usage
Const type Varibale = value or Type const variable = value; In C + + is commonly used to replace the # define, one reason is that the compiler processing the source code is removed by the preprocessor, the error is very debugging, the second reason is that each use will re-request memory, add code, and the const is only a portion of memory space.
2.2. use of a CONST finger pin
(1)The pointer itself is constant immutable
(char*) const pcontent;
Const (char*) pcontent;
(2)What the pointer points to is a constant immutable
Const (char) *pcontent;
(char) const *pcontent;
(3)both are immutable .
const char* const pcontent;
(4)there are different ways to line up along the * number:
If the const is on the left side of the * , the const is used to modify the variable pointed to by the pointer, that is, the pointer is constant;
if the const is on the right side of * ,const is the modifier pointer itself, that is, the pointer itself is a constant.
3 , use in functions CONST
(1) const modifier function parameters
A. The passed arguments cannot be changed within the function (meaningless because Var itself is a formal parameter)
void function (const int Var);
B. The contents of the parameter pointer are constant
void function (const char* Var);
C. The parameter pointer itself is constant immutable (also meaningless because char* var is also a formal parameter)
void function (char* const VAR);
D. Parameters are references, in order to increase efficiency and prevent modification. When modifying reference parameters:
void function (const class& Var); // reference parameters cannot be changed within a function
void function (const type& Var); // reference parameters are constants within a function immutable
(2) const modifier function return value
you can prevent users from modifying the return value. The return value is also to be paid to a constant or constant pointer accordingly.
4.Const modifier member function
Const objects can only access const member functions, and non-const objects can access any member function, including the const member function;
The members of a const object cannot be modified, and objects maintained by pointers can indeed be modified;
the const member function can not modify the object's data,regardless of whether the object is of a const nature. The compilation is checked against the modification of member data.
The const keyword in C + +