Overloading equality operator for classes

rumbu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 29 09:48:03 PST 2015


bool opEquals(Object obj, int value)
{
     //do something to compare them
     return false;
}


void main(string[] args)
{
     Object obj;
     if (obj == 12) {}
     //ERROR function object.Object.opEquals (Object o) is not 
callable using argument types (int)	
}

According to paragraph (2) - 
http://dlang.org/operatoroverloading.html#eqcmp), the compiler 
must try obj.opEquals(12) and 12.opEquals(obj) but this is not 
happening.

Is there any other way to overload the equality operator? (except 
overriding opEquals in each class intended to be compared with an 
integer)


More information about the Digitalmars-d-learn mailing list