www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Default initialization for classes

reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
I am slightly confused. As an exercise, I am writing a simple 3D vector 
class. Part of this involves playing with operator overloading. Here's 
some of the class:

class Vector3D {
public:
     real x, y, z;

     this(real x, real y, real z) {
         this.x = x;
         this.y = y;
         this.z = z;
     }
     this() { this(0, 0, 0); }

     Vector3D opAdd(Vector3D r) {
         return new Vector3D(x + r.x, y + r.y, z + r.z);
     }
}

This was my first stab at the opAdd function, and I think it looks most 
right. In poking around dsource, I have also seen such functions written 
like:

     Vector3D opAdd(Vector3D r) {
         Vector3D v;
         v.x = x + r.x;
         v.y = y + r.y;
         v.z = z + r.z;
         return v;
     }

(The example I'm thinking of is seen here:
http://www.dsource.org/projects/tutorials/wiki/CurrencyExample
)

So, where's v here? Is D implicitly doing "new Vector3D()"? Is this 
better, worse, or not different compared to what I did? Where is this 
mentioned in the spec? (I couldn't find it.)

-Kirk McDonald
May 20 2006
next sibling parent Oskar Linde <olREM OVEnada.kth.se> writes:
Kirk McDonald wrote:

 I am slightly confused. As an exercise, I am writing a simple 3D vector
 class. Part of this involves playing with operator overloading. Here's
 some of the class:
 
 class Vector3D {
 public:
      real x, y, z;
 
      this(real x, real y, real z) {
          this.x = x;
          this.y = y;
          this.z = z;
      }
      this() { this(0, 0, 0); }
 
      Vector3D opAdd(Vector3D r) {
          return new Vector3D(x + r.x, y + r.y, z + r.z);
      }
 }
 
 This was my first stab at the opAdd function, and I think it looks most
 right. In poking around dsource, I have also seen such functions written
 like:
 
      Vector3D opAdd(Vector3D r) {
          Vector3D v;
          v.x = x + r.x;
          v.y = y + r.y;
          v.z = z + r.z;
          return v;
      }
 
 (The example I'm thinking of is seen here:
 http://www.dsource.org/projects/tutorials/wiki/CurrencyExample
 )
 
 So, where's v here? Is D implicitly doing "new Vector3D()"? Is this
 better, worse, or not different compared to what I did? Where is this
 mentioned in the spec? (I couldn't find it.)

In the latter case, Vector3D seems to be a struct rather than a class, and as such, it is passed by value and not created with new. See more about the different semantics of structs and classes in D in the docs. /Oskar
May 20 2006
prev sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Kirk McDonald wrote:
 I am slightly confused. As an exercise, I am writing a simple 3D vector 
 class. Part of this involves playing with operator overloading. Here's 
 some of the class:
 
 class Vector3D {
 public:
     real x, y, z;
 
     this(real x, real y, real z) {
         this.x = x;
         this.y = y;
         this.z = z;
     }
     this() { this(0, 0, 0); }
 
     Vector3D opAdd(Vector3D r) {
         return new Vector3D(x + r.x, y + r.y, z + r.z);
     }
 }
 
 This was my first stab at the opAdd function, and I think it looks most 
 right. In poking around dsource, I have also seen such functions written 
 like:
 
     Vector3D opAdd(Vector3D r) {
         Vector3D v;
         v.x = x + r.x;
         v.y = y + r.y;
         v.z = z + r.z;
         return v;
     }
 
 (The example I'm thinking of is seen here:
 http://www.dsource.org/projects/tutorials/wiki/CurrencyExample
 )
 
 So, where's v here? Is D implicitly doing "new Vector3D()"? Is this 
 better, worse, or not different compared to what I did? Where is this 
 mentioned in the spec? (I couldn't find it.)
 
 -Kirk McDonald

This only works with sturcts. As Oskar said, structs don't need to be newed, they are by value. Now that I look at http://www.dsource.org/projects/tutorials/wiki/CurrencyExample I see that it's doing it on a class! The code must be borken.
May 20 2006
next sibling parent Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
Hasan Aljudy wrote:
 Now that I look at
 http://www.dsource.org/projects/tutorials/wiki/CurrencyExample
 I see that it's doing it on a class! The code must be borken.
 

You'll also notice that the opAdd method in that example takes two arguments instead of one. Apparently, since the method's never called in the code (it might as well be removed) the error hasn't been noticed.
May 20 2006
prev sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Hasan Aljudy wrote:
 This only works with sturcts. As Oskar said, structs don't need to be 
 newed, they are by value.
 
 Now that I look at 
 http://www.dsource.org/projects/tutorials/wiki/CurrencyExample
 I see that it's doing it on a class! The code must be borken.
 

Ah! That would do it. So the way I did it first is the proper way? -Kirk McDonald
May 20 2006
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message

 Ah! That would do it. So the way I did it first is the proper way?

Yes, that's the right way to do it for classes.
May 20 2006