The questions are directed toward the second line in main(). I ran this as displayed and compared it to removing the const and reruning. No difference. Why? No impact with a variable change????
What is actually kept constant when using const in this example? If the results are the same, then const has no influence.
Thanks.
// Exercise to observe pointer and const combinations.
What is actually kept constant when using const in this example?
The access path.
Given a modifiable object (Var), you may construct a const or a non-const access path (reference or pointer, possibly multilevel). The compiler will prevent you from using a const-qualified access path to modify the object, but it's perfectly OK to modify that object using a non-const access path.
Note that this is different from having a const object, e.g. constint Var. Doesn't matter if you have a pointer-to-const or pointer-to-nonconst, if it leads to the const object, modifying that object is undefined behavior.
Basically, the const is directed toward Var or &Var? If it is Var, then there should have been a compiler error when Var was reassigned. If it is &Var, then the results indicate const has no influence and therefore why include it?
Not sure how to relate your response to my question.
I mean the value of int, not its type. Types are always immutable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int main()
{
int Var = 18; // modifiable int
constint * pcVar = &Var; // pointer to a non-modifiable int
int * pmVar = &Var; // pointer to a modifiable int
constint& rcVar = Var; // reference to a non-modifiable int
int& rmVar = Var; // reference to a modifibale int
// *pcVar = 7; // illegal
*pmVar = 7; // legal
// rcVar = 7; // illegal
rmVar = 7; // legal
}