assign value to *this like in c++?

XavierAP n3minis-git at yahoo.es
Thu Jun 20 11:57:16 UTC 2019


On Thursday, 20 June 2019 at 11:28:53 UTC, lyan wrote:
> Hi!
>
> I just started with D, read already a bit through the 
> documentation but I'm currently stuck here at porting my 
> vector3 class to D:
>
> inline void normalize() {
>   *this /= length();
> }
>
> How can I achieve that in D? Also: is there a way to overload 
> /=, */ operators?
>
> Thanks for the help!

In C++ "this" is a pointer, but in D it's a reference -- so this 
should work:

this /= length();

BTW the empty () in function calls is optional in D; and see also 
"UFCS", which Bjarne S. wanted to get into C++ but ISO rejected 
it.

Yes it's possible to overload operators:

https://dlang.org/spec/operatoroverloading.html#op-assign

Note that these reserved functions to overload operators are 
templates. Therefore it's possible to generate code for several 
operators (e.g. + and - or more) with a single template and 
mixins; as shown in the examples in the link above.


More information about the Digitalmars-d mailing list