digitalmars.D - Default initialization for classes

• Kirk McDonald (33/33) May 20 2006 I am slightly confused. As an exercise, I am writing a simple 3D vector
• Oskar Linde (5/44) May 20 2006 In the latter case, Vector3D seems to be a struct rather than a class, a...
• Hasan Aljudy (6/47) May 20 2006 This only works with sturcts. As Oskar said, structs don't need to be
• Deewiant (5/9) May 20 2006 You'll also notice that the opAdd method in that example takes two argum...
• Kirk McDonald (3/10) May 20 2006 Ah! That would do it. So the way I did it first is the proper way?
Kirk McDonald <kirklin.mcdonald gmail.com> writes:
```I am slightly confused. As an exercise, I am writing a simple 3D vector
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); }

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 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
```Kirk McDonald wrote:

I am slightly confused. As an exercise, I am writing a simple 3D vector
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); }

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 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
Hasan Aljudy <hasan.aljudy gmail.com> writes:
```Kirk McDonald wrote:
I am slightly confused. As an exercise, I am writing a simple 3D vector
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); }

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 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
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

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
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
"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