My main blog: half an acre of Square pond
The main references of this paper come from: C + + Primer Chinese version (5th edition) 57th to 58th side
1. Top-level const and underlying const concept
As we know, the pointer itself is an object, because the pointer actually corresponds to a memory unit of the storage space, however, the pointer is also pointing to a data object, so the pointer is a constant and the pointer to a constant is two completely different concepts,top-Level constIndicates that theThe pointer itself is a constant,underlying constIndicates that theThe object that the pointer refers to is a constant, more generally,a top-level const can indicate that a random object is a constant, which is true for arithmetic types, classes, pointers, and so on, regardless of data type,The underlying const is related to the basic type part of a composite type such as pointers and references, the more special is that the pointer can be either the top-level const or the underlying const, which differs significantly from the other types.
2. The difference between the top-level const and the underlying const when running copy operations
For the top-level const and the underlying const, there is a distinct difference in running the object copy:
(1) Top-level const is unaffected
int i = 0;const int ci =; Can not change the value of CI, this is a top level consti = CI; Correct: CI is a top-level const, no effect on this operation const int *P2 = &ci; Agree to change the value of P2, which is an underlying constconst int *const p3 = p2; The const on the right is the top-level const, and the left is the bottom constp2 = P3; Correct: The type of object that P2 and P3 point to is similarly, the part of the P3 top-level const does not affect
(2) The limitations of the underlying const cannot be ignored, requires that the copied and copied objects have the same underlying const qualification or can be converted to the same data type, generally can be converted to a constant, and vice versa
int *p = p3; Error: P3 contains the underlying const definition, and p does not have p2 = p3; Correct: P2 and P3 are the bottom CONSTP2 = &i; Correct: int* can be converted to const int*int &r = CI; Error: Normal int& cannot bind to int constant on const int &R2 = i; Correct: The const int& can be bound to a normal int
To analyze the above code:
int *p = p3;
P3 is both a top-level const and a bottom-level const, when the object copy is run, the top const part does not have any influence, regardless of the effect, but P3 is also a bottom const, it requires the copied object has the same underlying const qualification, and p does not, so is wrong;
p2 = p3;
P3 require the object to be copied into the same level of const qualification, P2 is also a lower-level const, it is correct;
p2 = &i;
To I take address will getint*
, p2 isconst int*
, the former is a lot, the latter is a constant, assignment statement equals right side of the type to the left side of the conversion, the amount can be converted to the constant, it is correct;
int &r = ci;
From the left side we know that what is required is a reference that is bound to a CI, and the type of reference that is bound to the CI isconst int&
, the type of the left side of the equals sign isint&
, the type on the right side of the assignment statement is converted to the left, but the constant cannot be converted to a very good amount, so it is wrong;
const int &r2 = i;
From the left side we know that what is needed is a reference bound to I, and the type of reference bound to I isint&
, the type of the left side of the equals sign isconst int&
, the type on the right side of the assignment statement is converted to the left side, which is usually a lot to convert to, so it is correct.