How do you overload opEquals?

Dale Matthews dem7w2 at mst.edu
Tue Nov 19 12:59:30 PST 2013


I'm brand new to D and I'm taking on a project. In the midst, 
I've overloaded a whole load of operators for my Vec3 class. Most 
seem to be working (I haven't tested them all yet) but opEquals 
is refusing to be called. I've followed the guidelines here: 
http://dlang.org/operatoroverloading.html#equals

class Vec3
{
public:
	float x, y, z;
	this(float xVal, float yVal, float zVal) { x = xVal, y = yVal, z 
= zVal; }
	bool opEquals()(auto ref const Vec3 v) const
	{
		debug writeln("bool opEquals(Vec3) called");
		return x == v.x && y == v.y && z == v.z;
	}
};
unittest
{
	Vec3 v = new Vec3(5, 10, 15);
	Vec3 v2 = new Vec3(5, 10, 15);
	//if(v.opEquals(v2))                      //this works great!
	if(v == v2)                                   //idk where this 
goes off to but it returns false
		writeln("FINALLY");
	else
		writeln("try again?");
}

I've tried many variations of auto ref and const (although I'm 
not sure how the function is different for each variation), and 
it still doesn't get called. I feel like I'm missing something 
simple.


More information about the Digitalmars-d-learn mailing list