Tag: value Pop const space ACK assignment double operation article
Const Qualifier: The object is qualified with Const. This allows the const object to change its value once it is created.
A const object must be initialized when it is defined, in two ways:
const int x = 10;const int x = GetValue ();
The first is to assign a constant or constant expression to an object directly. Initialized in this way, the compiler replaces all objects in the compilation phase, such as: Replace all occurrences of x in the program with 10.
Another way to do this is to initialize it at execution time. Initializes x with the return value of the function GetValue ().
By default, const objects are only valid within a file. To use the same const object in all files, you can add extern when you define a const object. and use extern to declare the const object in other files, such as:
extern const int x = 10;//defines the const int type object in main.cc. and initialize. extern const int x; //Declaration of const int type object in Main.h.
a const reference:
When you initialize a const reference, you agree to use a discretionary expression as the initial value, such as:
int x = 10;double y = 10.4;const int &r1 = x;const int &r2 = 10;const int &R3 = x * 10;const int &r4 = y;
In the last three cases, the fact that R is bound to a temporary amount, the compiler will turn the code into such as the following:
int tmp = y;const int &R4 = tmp;
The value of R4 cannot be changed at this time, and the value of R4 will not change when Y is changed, because R4 is a temporary reference.
A const reference can refer to an object that is not const. The const reference simply qualifies for a reference-scoped operation, and is not qualified for the referenced object itself as a const object, such as:
int x = 10;const int &r = x;
pointers and Const:
pointer to constant: refers to the value of the object pointed to by the pointer can not be changed, the value of the pointer itself can change, that can again point to another object, such as:
const int x = 10;int y = 20;const int *p1 = &x;const int *p2 = &y; A pointer to a constant can point to a quantity of *p2 = ten; Error, you cannot change the value of the object pointed to by a pointer to a constant, even if the object itself is too much to
Const pointer:A constant pointer, which must be initialized when defined, and then no longer be able to point the pointer to another object, but with the ability to alter the value of the object pointed to by the pointer. Such as:
int x = 10;int * Const P = &x; You cannot change the value of the pointer itself. Can only change the value of a pointer to the object const int * Const P2 = &x; A constant pointer to a constant object that cannot alter the value of the object being referred to or alter the value of the pointer itself
constexpr and constant expressions:
A constant expression is an expression that does not change the value and can be evaluated during compilation. literals belong to constant expressions. A const object initialized with a constant expression is also a constant expression. Whether an object (or expression) is a constant expression is determined by its data type and initial value, only the const type. And the initialization value is a constant expression, the object is a constant expression.
In a complex system, it is very difficult to tell whether an initial value is a constant expression, as specified in C++11. Agree to declare the variable as a constexpr type so that the compiler can verify that the value of the variable is a constant expression.
A variable declared as constexpr must be a constant and be initialized with a constant expression, such as:
constexpr int MF =; A constant expression of 20 constexpr int limit = MF + 1; MF + 1 is a constant expression constexpr int sz = size (); only when size () is a constexpr function is the correct declaration statement
Generally, if you assume that a variable is a constant expression, declare it as a constexpr type.
C + + Const qualifier