Is this a operator overloading bug?

Tim Fang no at spam.com
Thu Jan 11 06:07:58 PST 2007


I have called "a.set(1,1,1)" to init the members in main().

"David Medlock" <noone at nowhere.com> Wrote
:eo5bif$2sr4$1 at digitaldaemon.com...
> Tim Fang wrote:
>>     I am very new to D and I am trying to implement a Vector3 struct,
>> please take a look at the code below, why the output is "nananan"? I use
>> dmd 1.00.
>>
>> --code---------------------
>> import std.stdio;
>>
>> void main()
>> {
>>  Vector3 a;
>>  a.set(1,1,1);
>>  a = a*2;
>>  writefln(a.x, a.y, a.z);
>> }
>>
>>
>> struct Vector3
>> {
>>  float x,y,z;
>>
>>  // constructor
>>  void set(float _x, float _y, float _z)
>>  {
>>   x = _x;
>>   y = _y;
>>   z = _z;
>>  }
>>
>>  Vector3 opMul(float s)
>>  {
>>   Vector3 ret;
>>   ret.x = x*s;
>>   ret.y = y*s;
>>   ret.z = z*s;
>>   return ret;
>>  }
>> }
>
> Its not a bug, you didn't initialize the x,y and z members of Vector3.
>
> try:
>
> struct Vector3
> {
>   float x =0, y =0, z =0;
> ...
> }
>
> -DavidM
>






More information about the Digitalmars-d mailing list