Int A=10;
Const * const int a=& amp; A;
+ + A;
STD: : cout & lt; <"Value1:" & lt; STD: : cout & lt; <"Value2:" & lt; <* a & lt;
VS2019 compiled the results for the
11 value1:
Value2:11
I understand A=10, means that A block of memory in A stack, c + + X temporarily become A memory, and memory to store an int value for X in 110;
X, then a pointer to a point to memory before and after the pointer is const, that is to say, the pointer can point to memory X, at the same time pointing to the memory of X for constant
Then * a +=1 is unable to compile, the compiler errors
But by + + A successfully changed the memory in the X value, the final output of * A also did change the
I want to ask next in the c + +, have a const pointer, before and after a point to a variable, how to understand this kind of behavior?
CodePudding user response:
All limits are in aHas nothing to do with A
CodePudding user response:
Const modifier which part, which part can't modifyFor example,
Int A=10;
Int B=15;
Int * const a =& amp; A;
//a=& amp; B;//const modifier a, so a can't change
* a=B; Can you change//
Int A=10;
Int B=15;
const int a=& amp; A;
//* a=B;//const modifier, * * can't change
A=& amp; B; Can you change//
Int A=10;
Int B=15;
const int * const a =& amp; A;
//a=& amp; B;//const modify *, and modify a, it is two parts cannot change
//* a=B;
Const just for it access restrictions, and modify the variables do not affect his variable access, so other variables still can be normal operation,
Are coupled with const is to make the the variable read-only, don't let it have write permissions, this will prevent through the variables to destroy the original
CodePudding user response:
Const is a limit to a can't change, as to the value of a variable constant, regardless of it