1, initialization
The const object must be initialized if its value cannot be changed once it is created.
The initial value can be arbitrarily complex expressions:
const int i = Get_size (); Correct: run-time initialization
const INT j= 42; Correct, compile-time initialization
const int k; Error: K is an uninitialized value
If you initialize another object with an object, it does not matter whether the object is const:
int i = 42;
const int ci= i; Correct: The value of I was copied to the CI
Const J = ci; Correct: The value of CI was copied to J
2, the const reference
You can bind a reference to a const object, just as you would bind to another object, which we call a reference to a constant. Unlike a normal reference yes, a reference to a constant cannot be used to modify the object to which it is bound.
const int CI = 42;
const int &R1 = CI; Correct: References and their corresponding objects are constants
R1= 42; Error: R1 is a reference to a constant and cannot be modified
int &r2 = CI; Error: An attempt was made to have a very important reference point to a constant object
Initialization and references to const
int i = 42;
const int &R1 = i; Allows a const int& to be bound to a normal int object
const int &R2 = 42; Correct: R2 is a constant reference
const int &R3 = R1*2; Correct: R3 is a constant reference
int &r4 = r1*2; Error: R4 is an ordinary, very trivial reference
You cannot change the value of I by changing the value of R1, but you can change the value of R1 by changing the value of I.
3. Pointers and const
The combination of pointers and constants can be combined into pointer constants, constant pointers, and pointer constants that point to constants (a combination of the first two). Which is the combination of the constant symbol const and the pointer symbol * In order to be judged. Const is a constant pointer before *, if * is a pointer constant before the const.
Const (*) on the left, I am the pointer variable pointing constant;
Const (*) on the right, I am the pointer constant pointing to the variable;
Const (*) on both sides, I am the pointer constant pointing constant;
(1) Pointer constants
int i = 0;
int * Const J = &i;
Pointer constants
A pointer constant refers to the pointer (address) is a constant, the pointer itself cannot be changed, and the value that the pointer points to can be changed. Use the top level const to denote that the pointer itself is a constant.
(2) Constant pointer
int i = 0;
int const * J = &i;
Constant pointer
A constant pointer is that the value (object) that the pointer points to cannot be changed, but the value of the pointer can be changed. Use a noun-bottom const to indicate that the object that the pointer refers to is a constant.
4, constexpr and constant Expressions
A constant expression is an expression that does not change the value and can get the result of the calculation in the compilation process. Obviously, the literal value is a constant expression, and the Const object is also a constant expression initialized with a constant expression.
Whether an object (or expression) is a constant expression is determined by its data type and initial value, for example:
const int max_files = 20; Is
const int limit = Max_files + 1; Is
int staff_size = 27; No
const int SZ = Get_size (); SZ is not a constant expression because the concrete value is to be obtained at run time
In a complex system, it is difficult to tell whether an initial value is a constant expression. Of course, you can define a const variable and set its initial value to one of the constant expressions we think of, but when we actually use it, we often find that the initial value and the very variable expression.
C++11 the new standard stipulates that the variable is allowed to be declared as a constexpr type so that the compiler verifies that the variable is not a constant expression. A variable declared as constexpr must be a constant, and it has to be initialized with a constant expression:
constexpr int mf=20; 20 is a constant expression
constexpr int limit = MF + 1; Mf+1 is a constant expression
constexpr int sz = size (); A correct declaration statement is only when the size is a constexpr function
5. Pointers and constexpr
If a pointer is defined in the CONSTEXPR declaration, the qualifier constexpr only valid for the pointer (becomes the top-level const), regardless of the object the pointer refers to:
const int *p= nullptr; P is a pointer to an integer constant, a constant pointer, the underlying const
constexpr int *q = nullptr; Q is a constant pointer to an integer, pointer constant, top const