class opBinary overloading. value + null and null + value
ref2401
refactor24 at gmail.com
Sun Oct 21 13:34:50 PDT 2012
public class MyClass
{
private int value;
this(int val)
{
value = val;
}
MyClass opBinary(string op: "+")(MyClass rhs)
{
if (rhs is null)
{
return null;
}
MyClass result = new MyClass(value + rhs.value);
return result;
}
}
void main()
{
MyClass v1 = new MyClass(4);
MyClass v2 = new MyClass(5);
auto v3 = v1 + null; // v3 is null
auto v4 = null + v2; // v4 equals v2
}
in this example v3 is null. i thought v4 would be null too, but i
was wrong.
what should i do to make my binary operation commutative?
More information about the Digitalmars-d-learn
mailing list