EDIT:
this question does NOT have an answer ^^there because it merely says
By declaring a variable as const you indicate compiler that you have no intentions of modifying that variable.
What does "modifying" mean? Changing the assigned value? What if it's a compound object - what about changing its data members (won't work), but if the data member is a reference changing it will work even if the containing object is const? Why?
I know that "Such object cannot be modified" and that "By declaring a variable as const you indicate compiler that you have no intentions of modifying that variable." I'm interested in what is meant by "modifying" when the object is not a basic type but is a compound type and besides its value data members it has references and pointers, too.
cppreference only says
Such object cannot be modified
Is "object" or "modified" a technical term associated with the memory representation or something? Experimenting shows that if the object data members are pointers or references the data represented by them can be modified.
class Vector {
public:
Vector () : ptr(new int[20]) {}
void f() const {
ptr[0] = 100;
}
private:
int* const ptr;
};
the code above compiles, doing const Vector v;
does not prepend const to the int* const ptr
.
Also if the data members are references, who knows how the reference is internally implemented (if it's not specified in the standard), what does then const
do according to the standard?
MY ANSWER: (also thanks to info in comments)
When you declare your object const
const MyObject obj();
or
__class definition__
private:
const MyObject obj;
you basically say the data, that are directly part of the object in memory, cannot change. Remember that sub-objects (data members), sub-sub-objects,... are directly part of the object in memory.
Thus in compound objects, it “marks” all members constant too, in a “deep const” manner, until it descends to non-compound types only.
But remember that pointers and reference don’t store the object, they store only the memory location. So for pointers you get a constant pointer and for references nothing changes (the memory location they hold, they point to, is always constant).
So a quick summary of what happens if you declare a compound object const.
for its data members:
- object is of a primitive data type (but non-pointer and non-reference) -> answer ^^there
- object is a pointer -> SomeObj* becomes SomeObj* const
- object is a reference -> SomeObj& stays same
- object is a compound data type -> “mark” all its members const with these rules
Although why it is designed that way for references might not be evident. So one should NOT think of a reference as a black box type that can be implemented in ANY way.
One HAS to think references are just pretty constant pointers, having merely their implementation in mind rather than a reference black box. Otherwise it would never make sense, references aren’t closer to value types than to pointers, they are pointers. Syntactically they might be like value types but semantically and implementation-wise they are constant pointers.