Overriding opEquals in classes, for comparison with things that aren't Objects

pineapple via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 29 07:00:59 PST 2016


With this bit of code, the first method seems to work fine - 
things go as expected. But I get a compile error with the second 
method, and I'm not sure how else to write this.

     override bool opEquals(Object value) const{
         return this.equals(cast(typeof(this)) value);
     }
     override bool opEquals(T)(T value) const if(isNumeric!(T)){
         return this.equals(value);
     }

The compile errors I get look like this:

C:\path\to\file.d(477): Error: function 
units.quantity.opEquals!int.opEquals cannot override a 
non-virtual function
C:\path\to\file.d(519): Error: template instance 
units.quantity.opEquals!int error instantiating

How can I revise this so that I can override comparison operators 
for things other than class instances?



More information about the Digitalmars-d-learn mailing list