Operator "+=" overloading for class?

Ki Rill rill.ki at yahoo.com
Mon Dec 18 14:38:14 UTC 2023


On Monday, 18 December 2023 at 04:49:53 UTC, novice2 wrote:
> On Monday, 18 December 2023 at 03:39:16 UTC, Ki Rill wrote:
>> On Sunday, 17 December 2023 at 07:05:12 UTC, Adam D. Ruppe 
>> wrote:
>>> On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote:
>>>> [...]
>>>
>>> check what `op` is. pretty sure it is "+" not "+=" so your 
>>> element isnt' saved anywhere. also a bit iffy there isn't a 
>>> member here to work on
>>
>> Yes, op is '+'. What do you mean by it isn't saved anywhere?
>
> your code just return result value,
> but it should not return but save result to "this"
> see example at 
> https://dlang.org/spec/operatoroverloading.html#index_op_assignment

I get an error, but don't understand why.
```d
auto opOpAssign(string op)(Value rhs)
{
     this = this + rhs;
     return this;
}

// Error: `this` is not an lvalue and cannot be modified
```


More information about the Digitalmars-d-learn mailing list