Briefly recall the nature of the constants:
main int () { const int BUFFSIZE = 512; BUFFSIZE = 512; // × BUFFSIZE is constant }
Initialized:
const int i = get_val (); // √ runtime initialization const int j = 42; // √ initialized at compile time const int k; // × k uninitialized
When an object is used to initialize another object, they are not irrelevant const
you i = 42; const you ci = i; you j = ci;
Will play a role in shaping ci is a constant, but only constant feature ci ci changed in execution of the operation
const and reference #
Constant references to #
The reference is bound to a const object, called the reference to the constant
A reference to the constant can not be used to modify the object it is bound object references and references are constants
const int ci = 1024; const int &r1 = ci;
have to be aware of is:
1024 = CI int const; const int & R1 = CI; R1 = 42 is; // × R1 is a reference constant & r2 = ci int; // × r2 is a const reference, ci is a constant object
Not allowing the ci used to modify the object it is bound, so it can not be changed by reference to ci (assuming that the fourth sentence lawful, then we can go to the changed ci by r2, is obviously wrong)
Similarly following two sentences
int &r3 = r1; //× const int &r4 = r1; //√
Our oral said constant reference is actually a const reference , strictly speaking, there is no constant references, because pointers and not the same, reference is not an object , we have no way to get it is given the same reference itself
(PS: Because C ++ does not allow arbitrary object reference is bound to change, so it can be understood that all references are constants, of course, whether the object reference is a constant, it can decide to participate in the operation, but no matter how will not affect the binding relationship between the reference and object)
Initializing a reference to the constant #
We know the type of the referenced object type and must be referenced by the same, but involves initialization constant references will appear the second exception (the first: initialization constant reference is allowed to use any expression as an initial value, as long as the expression can converted into the type of reference)
42 is I = int; const int & R1 = I; // const int √ allow the target to bind to a normal int const int & r2 = 42; // √ r2 is a constant reference const int & r3 = r1 * 2 ; // √ r3 is a constant reference int & r4 = r1 * 2; // × r4 is a very common reference amount
Why does this happen? Let's look at a simple example
double dval = 0.114514; const int &ri = dval; cout << "ri = " << ri <<endl;
Run output
ri = 0
In this process, in fact, changed the code compiler:
double dval = 0.114514; const int temp = dval; const int &ri = temp; cout << "ri = " << ri <<endl;
In this case, ri bound to a temporary amount of objects , which you can read at the above code what is happening right
You can imagine the following, if ri is not constant, the implementation of the above-mentioned initialization process will be what kind of consequences: If ri is not constant. Ri allows for the assignment, which would change the value of the object referenced by ri (at this time the amount of the binding is temporary and not dval), so that C ++ also the following behaviors classified as illegal
double dval = 0.114514; int &ri = dval; //× cout << "ri = " << ri <<endl;
Also note that for const reference may not reference a const object
Const reference only to the reference operation may be involved to make a defined, a reference to whether the object itself is not made a constant is defined, the object may also be a non-constant, allowed to change its value by other means
42 is I = int; int & R1 = I; const int & R2 = I; // R2 = 0; // × R2 is a constant reference COUT << "R2 =" << endl << R2; I = 0; COUT < < "r2 =" << r2 << endl;
The program outputs the following:
42 r 2 =
r 2 = 0
const and pointers #
Constant pointer pointing to #
Similar to a reference constant , pointing constant pointer can not be used to change the value of the object to which it refers
At the same time, the address you want to store the constant object, can only point to the constant use of pointers:
Double HOMO = 1.14 const; Double & HOMO * = PTR; // × PTR is pointer to a common const * cptr = Double & HOMO; // √ CPTR = 5.14 × // not to assign * cptr
Unlike a reference, we can change point constant pointer to object points
Double HOMO = 1.14 const; const * Double CPTR = & HOMO; COUT << "CPTR =" << endl << CPTR * const Double homo2 = 5.14; CPTR = & homo2; COUT << "CPTR =" CPTR << << * endl; // to point to allow a constant amount of the object is a pointer to
Note that, similar to a reference, although we say that the type of the pointer must be consistent with the referent, but here are the first exception: Allows a pointing constant pointer pointing to a great amount of objects
Double HOMO = 1.14 const; const * Double CPTR = & HOMO; Double dVal = 3.14; CPTR = & dVal; // to point to allow a constant pointer to a great amount of objects * cptr = 0.0 // but does not allow modifications by pointing a pointer to a constant value-const object
Therefore, pointing to a predetermined constant pointer nor the object to which it refers must be a constant , a so-called point not only requires constant by modifying the value of the pointer of the pointing object, and no object is not referred to a predetermined value by changing other means
Double HOMO = 1.14 const; const * Double CPTR = & HOMO; COUT << "CPTR =" << endl << CPTR * Double dVal = 5.14; CPTR = & dVal; // to point to allow a constant pointer to a great amount of objects / / * cptr = 0.0 // const but not modify the object by pointing to a constant pointer value COUT << "CPTR =" << endl << * CPTR; dVal = 0.0 // value of the object may be referred to by other means change cout << "cptr =" << * cptr << endl;
Now we output becomes:
cptr = 1.14
cptr = 5.14
cptr = 0
const pointer #
And different reference, pointer itself is the object, so as to allow the pointer itself constant, i.e. constant pointer , constant pointer must be initialized, and after completion of the initialization value (stored in the address pointer in the object) can not be changed
* Const keyword before the discharge , for explaining a pointer is constant (i.e. the constant value of the pointer itself)
errorNumb = 0 int; int const * = & curErr errorNumb; // curErr is a constant pointer always points errNumb const Double PI = 3.1415; const const * Double & PIP = PI; // PIP is a constant const pointer to the object
Two very different wording:
int * const curErr = & errorNumb; // curErr been directed errNumb * = curErr. 1; // can modify the value of a variable referred to
const int * curErr = & errorNumb; // curErr is a constant pointer * curErr = 1; // × referents can not be modifying the value of curErr
Const top and bottom const #
Since the pointer itself is an object, it can point to another object, and therefore the pointer itself is not constant , and the object pointer is not constant is two independent problems, top const represents a pointer itself is a constant, underlying const pointer indicating within the meaning of the object is a constant
In fact, it can represent any top level const object (itself) is constant, Analog more special, because it may be a top may be the underlying const
0 = I int; int const * = P1 & I; // P1 itself is constant, the top layer const const int = 42 is CI; // CI itself is constant, the top layer const const int * & P2 = CI; * after const //, p2 is constant pointer pointing to the underlying const const const int * p2 = P3; // look at the top left, bottom right is the look, p3 is a constant in a constant pointer pointing const int & r = ci; // const reference statement are bottom const, r is a constant reference
Copy operation will not affect the value of the copy of the object, the top layer is not affected const
and = you; p2 = p3;
But the underlying const will have restrictions:
When the copy operation, the copy-in and copy the object must have the same underlying const qualification alive two types of data objects can be converted (the amount of energy is converted into a constant, not vice versa)
int * p = p3; // × p3 comprising bottom const, p is not const int * p = p3; // √ p and p3 are bottom const P2 = p3; // √ P2 and p3 are bottom const P2 = & I ; // √ int int * can be converted into const int & CI = R & lt; // normal int & × not be tied to a const int & const int & r2 = i; // √ const int & can be tied to a common int