Thread overview
this in structs
Jun 03, 2004
Tobias Neukom
Jun 03, 2004
J Anderson
Jun 03, 2004
Ben Hinkle
June 03, 2004
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

June 03, 2004
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/
June 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".