Why use const? Code written with symbolic constants is easier to maintain, and pointers are often moved by reading edges rather than writing edges; many function references are read-only. The most common use of const is as an array of bounds and switch sub-condition designators (which can also be substituted with enumerators), such as the following:
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 (form) 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 the class name is a type descriptor of its own definition can be interchanged.
Such as:
First, Const function
For example, see the following table:
Second, the use of the Const method
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 (form) const
Constant group: type specifier const array name [size]
Constant pointer: const type specifier * Pointer name, type specifier * Const pointer name
2.1. How to use constants
Const type Varibale = value or Type const variable = value; Often used in C + + to replace # define, one reason is that the compiler is handling the source code that was moved by the preprocessor. The error occurs when it is very debugged, and the second reason is that each time it is used, the memory is applied again, the code is added, and the const is just one 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 also the different ways to line up along the * number:
Assuming that 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;
Suppose the const is on the right side of * . A const is a modifier of the pointer itself. That is, the pointer itself is a constant.
3 , use in functions CONST
(1) const modifier function parameters
A. The passed-in parameters cannot be changed within a function (meaningless, since Var itself is a form of participation)
void function (const int Var);
B. The content of the reference pointer is a constant variable
void function (const char* Var);
C. The reference pointer itself is a constant invariant (also meaningless, since char* Var is also a part of the form)
void function (char* const VAR);
D. The number of references is reference. In order to add efficiency at the same time prevent changes. 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 immutable within a function
(2) const modifier function return value
ability to prevent users from changing the return value. The return value also corresponds to a constant or constant pointer.
4.Const modifier member function
Const object can only access the const member function. Rather than a const object, you can access random member functions. Contains the const member function;
The members of a const object cannot be changed, while objects maintained by pointers can be changed;
the const member function is not able to alter the object's data. regardless of whether the object is of a const nature.
The compile time is checked against changes to the member data.
Constkeyword in C + +