C ++ top and bottom const const

Top const: represents the pointer itself is constant.

Bottom const: a pointer that points to the object is a constant.

And pointer type may be either the top layer may be a bottom const const

int i = 0;

int const * p1 = & i; // p1 can not be changed, which is a top const

int const p2 = 4; // p2 can not be changed, as a top const

int cosnt * p3 = & p2 // p3 can be changed, as the underlying const

const int * const p4 = p3; // const is a first bottom layer the top layer cosnt second const const, where const p4 i.e. the top layer is the bottom layer const.

In the implementation of the copy operation, the top-level const is not affected, but the underlying const greatly affected, copies of the underlying operations of const, and was admitted to test out the object will need to have the qualifications of the same underlying const.

int * p = p4 // erroneous operation, p4 const comprising the underlying meaning of referents const int, int and p is an ordinary

and:

p3 = p4 // correct, both the underlying

Additional, in c ++ 11, to determine a constant expression can be used to declare variables constexpr.

 

Guess you like

Origin www.cnblogs.com/zhangli07/p/11960242.html