digitalmars.D - this in structs
- Tobias Neukom (46/46) Jun 03 2004 Hi
- J Anderson (4/6) Jun 03 2004 Yes! It's done for constancy with classes.
- Ben Hinkle (6/7) Jun 03 2004 The documentation doesn't say but it looks like it is. I somehow had the
Hi
Why doesn't this work:
struct Vec3
{
float x, y, z;
static Vec3 opCall(float x, float y, float z) {
Vec3 v;
v.x = x;
v.y = y;
v.z = z;
return v;
}
Vec3 opMul(float v) {
return Vec3(x * v, y * v, z *v);
}
Vec3 opDiv(float v) {
float oneOverV = 1.0f / v;
return this * oneOverV;
}
}
DMC output:
incompatible types for ((this) * (oneOverV)): 'Vec3 *' and 'float'
While this works:
class Vec3C
{
public float x, y, z;
public static Vec3C opCall(float x, float y, float z) {
Vec3C v;
v.x = x;
v.y = y;
v.z = z;
return v;
}
public Vec3C opMul(float v) {
return Vec3C(x * v, y * v, z *v);
}
public Vec3C opDiv(float v) {
float oneOverV = 1.0f / v;
return this * oneOverV;
}
}
Is this a pointer when using structs ?
Cheers Tobias
Jun 03 2004
Tobias Neukom wrote:Is this a pointer when using structs ? Cheers TobiasYes! It's done for constancy with classes. -- -Anderson: http://badmama.com.au/~anderson/
Jun 03 2004
Is this a pointer when using structs ?The documentation doesn't say but it looks like it is. I somehow had the impression 'this' wasn't allowed in stucts but looking over the doc I don't know why I ever thought that 'cause it sure looks ok now. I suppose the section in the documentation about 'this' should mention for structs a pointer is used instead of a reference. Or maybe that's what Walter meant by "this resolves to a reference to the object that called the function".
Jun 03 2004









J Anderson <REMOVEanderson badmama.com.au> 