assign value to *this like in c++?

Basile B. b2.temp at gmx.com
Thu Jun 20 11:55:53 UTC 2019


On Thursday, 20 June 2019 at 11:31:19 UTC, lyan wrote:
> 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!
>
> The code is C++ btw. and I had a typo below, I meant /= and *= 
> operators.

Use `void opOpAssign(string op, T)(auto ref T t);` model to 
overload "DivAssisng" and "MulAssign". The compiler will 
instantiate the function template with the right `op` and `T` 
info when used in the code. These template parameters can be 
tested in a `static if` expression. See 
http://ddili.org/ders/d.en/operator_overloading.html for learning 
D operator overloads.

Note that these kind of questions should go in .learn : 
https://forum.dlang.org/group/learn


More information about the Digitalmars-d mailing list