Operator "+=" overloading for class?

Alexandru Ermicioi alexandru.ermicioi at gmail.com
Mon Dec 18 23:33:00 UTC 2023


On Sunday, 17 December 2023 at 04:13:20 UTC, Ki Rill wrote:
> I am trying to overload `opOpAssign` for my class. The code 
> compiles, but it does not seem to be working.
>
> ```d
> // binary operations have already been implemented for Value
> // i need +=, -=, *=, /=
> auto opOpAssign(string op)(Value rhs)
> {
>     mixin("return this" ~ op ~ "rhs;");
> }
>
> auto opOpAssign(string op)(in ElementType rhs)
> {
>     mixin("return this" ~ op ~ "rhs;");
> }
> ```
>
> What am I missing here? Full project code can be found 
> [here](https://github.com/rillki/tiny-grad).

Perhaps:
```d
auto opOpAssign(string op)(Value other) {
mixin("this.v " ~ op ~ "= other.v;");
return this;
}
```


More information about the Digitalmars-d-learn mailing list