Recently I learnt that:
During construction of an object, if the value of the object or any of its subobjects is accessed through a glvalue that is not obtained, directly or indirectly, from the constructor's
this
pointer, the value of the object or subobject thus obtained is unspecified.extern struct D d; struct D { D(int a) : a(a), b(d.a) {} // b(a) or b(this->a) would be correct int a, b; }; D d = D(1); // because b(d.a) did not obtain a through this, d.b is now unspecified
I also learnt here that:
in the example above,
d
and*this
are aliases for the same object.
So my question is that is there a reason for why when d
and *this
are aliases for the same object, then we're not allowed to use d
but allowed to use this
? What problem does using d
instead of this
raise? And how does using this
resolves that problem?