Reputation: 4375
struct S { string s; }
void method()
{
const S s = { "s" };
s.s = "l"; // Error
}
I can't understand why a compile-error is being generated here. From my understanding, making a struct-referencing variable const
should make the variable itself immutable (only s = { "m" }
after s
initialization should generate error), not the structure itself (so s.s = "l"
should pass OK). Why const makes both the variable and the struct immutable?
Upvotes: 1
Views: 62
Reputation: 240472
It's not "a variable referencing a struct instance". There's no indirection. The value of s
is a struct S
, which includes all of its fields.
Upvotes: 3