C ++ how to distinguish between top and bottom const const

https://blog.csdn.net/qq_19528953/article/details/50922303

A pointer actually defines two objects, the object pointer itself, and it refers. Both objects can be defined with the const

When the pointer itself is defined, it referred to as a top pointer const;

When the pointed-to object is defined as a constant, while the pointer itself is not defined, said bottom pointer const

const declaration can be placed before and after the type name will do, const int and int const are equivalent.

1. Statement pointer is pointing to the bottom of the constant const

int A = . 1 ;
 const  int * pa = & A; // bottom const 
* pa = 2 is wrong because the underlying object const pointer is defined, you can not change the content of the object 
content pa pointer points is not constant, it can be by assignment statement 
a = 2 are possible

2. pointer constant

Representative pointer itself, the definition must be initialized, which stores the address value can not be changed. const pointer must be placed behind the symbol * declared as const *

. 1  int A = . 1 ;
 2  int B = 2 ;
 . 3 * const  int PA = & A; // top pointer 
. 4 PA = & B; wrong, constant pointer stored address value can not be changed

So what is the role of distinction between the two?

There are underlying constraints, can not be assigned to the constant const const const underlying executed when a copy of the object. In other words, as long as you can correctly distinguish between top and bottom const const, you will be able to avoid such an assignment error

. 1  int num_c = . 3 ;
 2  const  int * p_c = & num_c;   // p_c pointer const the bottom
 . 3  // int * = P_D p_c;   // error, can not be assigned to the bottom pointer const const pointer unprimed 
. 4  const  int * = p_c P_D; // properly, it can be copied to the underlying bottom pointer const const pointer

2 when using the named cast const_cast conversion functions, need to be able to distinguish the top layer and the bottom layer const const, since only changes in the underlying const_cast const operands.

. 1  int num_e = . 4 ;
 2  const  int * = & p_E num_e;
 . 3  // * = p_E. 5;   // error, can not change the contents of the underlying pointer const 
. 4  int * P_F const_cast = < int *> (p_E);   / / right, const_cast can change the underlying const operands. But be sure to use num_e not know the type of const. 
5 * P_F = 5 ;   // correct, top-level non-const pointer pointing to the content may be changed 
. 6 COUT << num_e;   // output 5

 

for example

. 1  const  int A = . 1 ;  
 2  // int * PI = & A;   // error, & a is the bottom const, can not be assigned to the unprimed const 
. 3  const  int * PI = & A; // correct, & a is the bottom const, can be assigned to the underlying const 
. 4  const  int * const * const PPI PI = &   // i.e. const is the bottom, the top layer is const 
. 5  const  int   * const * const * = & PPPI PPI; // bottom const

 

Guess you like

Origin www.cnblogs.com/mch5201314/p/11485800.html