C + + primer in the top-level const and the underlying const previously did not understand the top-level const and the underlying const, this time to see the sense of understanding.
First, theconst is a qualifier, and the value of the variable it modifies cannot be changed .
For The general variable, there is no top-level const and the underlying const difference , and only to the pointer such a compound type of basic variables, there is such a difference.
how to distinguish the top-level const and the underlying const
pointers If you add a const modifier in two cases:
1 pointers to constants: Represents a pointer that cannot change its point to the content. When declaring a const can be placed before or after the type name, in the case of the int type, when declared: const int and int const are equivalent. To declare a pointer to a constant, which is the underlying const, give an example:
- int num_a = 1;
- int Const *P_A = &num_a; //Bottom const
- *p_a = 2; Error, pointer to "constant" cannot change the object being referred to
Note: A pointer to "constant" does not mean that the content it points to must be constant, but that the representation cannot be changed by the dereference (operator *). In the above example, the pointer p_a to the content is not a constant, you can pass the assignment statement: num_a=2; To change what it points to.
2 pointer constant : The pointer itself is a constant, which must be initialized when declared, and the address value it stores can no longer be changed. When declaring a const must be placed behind the pointer symbol *, that is: *const. Declaring a constant pointer is the top-level const, here's an example:
- int num_b = 2;
- int *Const P_B = &num_b; //top-level const
- P_b = &num_a; Error, constant pointer cannot change stored address value
In
fact, the top-level const and the underlying const are simple. 1. Adding a const qualifier to a pointer itself is the top-level const. ( the pointer is limited to the top )
2. Adding the const qualifier to the object that the pointer refers to is the underlying const. ( object is limited to the bottom)
Two distinguishing the role of the top-level const and the underlying constThere are two effects after differentiation.
1
there is a limit to the execution of object copies, and the underlying const of a constant cannot be assigned to a very low-level const. In other words, you can avoid such assignment errors if you can correctly differentiate between the top and bottom Const. Here's an example:
- int num_c = 3;
- const int *p_c = &num_c; //p_c is a pointer to the underlying const. Remember that you cannot modify the value of a variable by P_c
- int *p_d = P_c; Error, cannot assign the underlying const pointer to a non-underlying const pointer
- const int *p_d = P_c; //correct, you can copy the underlying const pointer to the underlying const pointer
2Use a named
When you force a type conversion function to const_cast, you need to be able to distinguish between the underlying const and the top-level const, because const_cast can only change the underlying const of the operand。 Here's an example:
- int num_e = 4;
- const int *p_e = &num_e;
- *p_e = 5; Error, cannot change what the underlying const pointer is pointing to
- int *p_f = const_cast<int *> (p_e); //Correct, const_cast can change the underlying const of the operand. But be sure to know that num_e is not a const type when used.
- *p_f = 5; //correct, non-top-level const pointer can change the point to the content
- cout << num_e; //Output 5
Top-level const and underlying const understanding in C + + primer