Will creating this new object, v, result in an entirely new object or simply the same object space referenced by *this?
Vector2 v = *this;
I would not expect it is actually creating a new object, but I’m seeing this code in a sample app that then manipulates this new object even though this code is in a class function that is set as const. Thus I would expect that this object,v, is in fact entirely a separate memory space than *this.
It’s creating a new object, yes – but by initialization, not assignment.
You can however bypass that by using references,
Vector2& v = *this;, in which casevis an alias for*this(orconst Vector& v = *this, if the method isconst).The reasons for this is exactly the one you stated – since the method in which this happens is
const, there’s no way to mutate*this– so you need a non-constcopy of it.