www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - this in structs

reply Tobias Neukom <tneukom swissonline.ch> writes:
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
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Tobias Neukom wrote:

 Is this a pointer when using structs ?

 Cheers Tobias
Yes! It's done for constancy with classes. -- -Anderson: http://badmama.com.au/~anderson/
Jun 03 2004
prev sibling parent Ben Hinkle <bhinkle4 juno.com> writes:
 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