1. The const modifier variable, which indicates that the value of the variable does not change:
Const TYPE ValueName = value;
2. The pointer uses the const
(1) The pointer itself is constant, with the following two types of notation:
(char*) const pName;
(2) The pointer points to the content is constant, there are the following two ways:
(char) const *pname;
Const (char) *pname;
(3) both are not changed
const char* Const pName;
3. const modifier Function parameters
Parameters cannot be changed within a function: void
function(
const
int
Var);
The content pointed to by the parameter pointer cannot be changed within the function: void
function(
const
char
* Var);
The parameter pointer itself is a constant that cannot be changed: void
function(
char
*
const
Var);
Parameters are references that increase efficiency while preventing modification: void
function(
const
TYPE& Var);
4. const modifier function return value
const
int
* fun2(),调用时const int *pValue = fun2();//我们可以把fun2()看作成一个变量,即指针内容不可变
int
*
const
fun3(),
调用时int * const pValue = fun2();
We can think of fun2 () as a variable, that is, the pointer itself is not mutable
C + + CONST Keyword Summary