Operator "+=" overloading for class?

Ki Rill rill.ki at yahoo.com
Thu Dec 21 01:43:40 UTC 2023


On Wednesday, 20 December 2023 at 02:56:24 UTC, Steven 
Schveighoffer wrote:
> Instead you are trying to reassign the `this` reference, which 
> is a local (and also forbidden). Think about it a second, your 
> `this + rhs` is going to allocate a *new* object. Then if the 
> assignment to the local `this` parameter succeeded, what 
> happens outside the member function? The true reference that is 
> calling this will not be updated!
>
> The only way to do this I can see is to reimplement for op=, or 
> maybe perform the operation and swap the guts out.
>
> -Steve

Thank you for such a detailed explanation! That does make sense.


More information about the Digitalmars-d-learn mailing list