Operators overloading in D2 again

Dan daniele.niero at gmail.com
Sat May 1 23:14:41 PDT 2010


Hi everyone,

is there anyway to do this with operators overloading? :


class Tester
{
	double x = 0.0;

	double opBinary(string op:"+")(double value)
	{
		return x+value;
	}

	Tester opBinary(string op:"+")(Tester other)
	{
		Tester ret;
		ret.x += other.x;
		return ret;
	}
}

int main(char[][] args)
{
	Tester t1 = new Tester;
	Tester t2 = new Tester;

	t1.x = 1.0;
	t2.x = 2.0;

	Tester t3;
	t3 = t1+t2;
	assert (t3.x = 3.0);

        return 0;
}


Thanks,
     Dan


More information about the Digitalmars-d-learn mailing list