Zookeeper
Analysis of several cases with the const keyword
Const modified code |
Meaning (features) |
Equivalence |
Int * P = & num; |
1. You can read yourself 2. You can change yourself through * P. 3. You can view other users through P = & Data |
Maximum Permissions |
Cons int * P = & num; |
1. Putting const on the left means that it points to a constant, which cannot be modified, 2. p = & data; (the address can be modified) 3. * P = 30; (this time is incorrect) |
The two are equivalent (Application: View others' accounts) |
Int const * P = & num; |
(Equivalent to the above) the limit can only be read, cannot be modified, and the address can be moved |
Int * const P = & num; |
You cannot modify your own address, but you can use * P to modify your own value. |
|
Const int * const P = & num; |
You cannot modify the address to view other values or modify your own values. You can only view your own values. |
1. You can view yourself 2. Do not change yourself 3. Do not look at others |
Int const * const P = & num; |
Only your account can be read and not written. |
Values can only be assigned during initialization (const limits that variables cannot be modified randomly)
Const Summary
On the left side of '*', const indicates that I am pointing to a constant.
On the right side of '*', const indicates a constant of the pointer.
You cannot change the above P = & data; to * P = 3. As follows:
Int const * P
Int * const P; you cannot modify your own address, but you can use * P to modify your own value.
You can modify it in * P mode.
6. Const int * const P = & data; in this way, you can only view your own data, neither modify others nor change yourself.
You can only view yourself: