Article Directory
const are constants
eg.
const int con = 1024;
the definition of an int constant
value of the variable con unchangeable
Assign a function
const int con = func();
Continuous variables declared
When const declaration continuous variables, are valid
const int con1=1, con2=2, con3=3;
con1, con2, con3 are constants
const reference and const type &
The constant reference point to the constant itself must also be a constant.
The reference variable can not be changed.
const int con1 = 1024;
const int &con2 = con1; //对常量的引用,简称常量引用,也必须是一个常量
const int &con3 = 1024; //常量引用,可以赋上一个字面值。而非常量引用是不行的
int cVal = 1;
const int &con4 = cVal; //常量引用,可以赋值一个非常量的变量
Note, con4, is assigned on a great value. con4 itself can not be changed, but points to the very magnitude is variable. Modify the very values:
cVal = 197;
cout << con4 << endl;
Outputs 197
The constant reference point to the constant itself must also be a constant
point of constant const reference, the magnitude of change is very, reference values are changed
not by itself to change the value of the points
const pointer
Const pointer: const type *
Constant pointer, is essentially a pointer. Declaration defines: first character constant const, followed by pointer type.
Pointing to a constant pointer itself must be a constant.
However, the pointer variable itself can be reassigned.
const int con1 = 1024;
const int *con2 = &con1; //指向常量的指针
int cVal = 1;
const int *con3 = &cVal; //指向非常量的指针
int cVal2 = 197;
con2 = con3 = &cVal2;
cout << *con2 << endl;
cout << *con3 << endl;
As described above, shaped like a const type *
variable definition, whether a point is constant, the amount is very. Can be re-designated address pointing.
Changed to point to the original value by the reference symbol pointer solutions, it is not acceptable.*con3 = 200;//error
Pointer constants: type * const
Pointer constant. It is essentially a constant that is a pointer type. Declaration defines: first pointer type, followed by the constant character const.
When you define it must be initialized. Because the pointer is a constant, so it can not be reassigned, it will always point to the initial value of the address.
int con1 = 1024;
int *const con2 = &con1; //常量指针
int cVal = 1;
*con2 = 10;
con2 = &cVal; //error
Above, CON2 address can not be changed, but the address point value is variable.
Immutable pointer itself, and does not address point value variable: const type * const
Comprehensive two definitions above,
int con1 = 1024;
const int *const con2 = &con1;
int cVal = 1;
*con2 = 10; //error
con2 = &cVal; //error
Above, CON2 address can not be changed, and does not address point value variable.
Top and bottom const const
Top const (top-level const) represents itself is a constant
underlying const (low-level const) represented by points is a constant
popular speaking,
top const, i.e. (pointer or reference, or other type) variable itself is not changed;
bottom const , i.e., (or other type of pointer or reference) value of the variable points immutable object.
For pointers which:
type top layer is const *;
const * is the underlying type.
A reference to it:
const of the type & is the underlying. (This provision, so I am also very surprised. Obviously you can point to the constant and non-constant, and by constant reference variables, not change itself, can not change the value of the object references can only say that this is a dead specified)
Only the top layer is the bottom const:
const int *const ccp = &cv;//左边是 low const,右边是 top const;
cpp11: constexpr constant expression
cpp11 Add keywords constexpr
can be used to declare constants, functions and constants.
constexpr int rv() {
return 100;
}
constexpr int xd = 88;
constexpr int xxd = rv();
The compiler during compilation verification, constant definitions are correct.
Since the constant value to be determined during the compile, it is available for non-basic types of constant expressions. Like a pointer type is not enough