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:56:56 PST 2016


On Friday, 29 January 2016 at 15:13:45 UTC, Mike Parker wrote:
> The first implementation is fine because you're overriding the 
> implementation in the base class (Object). However, the second 
> one fails because it's a template. Templates are non-virtual 
> and cannot override anything. Even if you could, there is no 
> such implementation in Object and, therefore, nothing to 
> override.
>
> Templated functions can be used as overloads, though, but I'm 
> not sure off the top of my head if the compiler accepts 
> templated opEquals on classes at all. My guess is no, but you 
> can find out by removing the override from the template 
> declaration. If not, you should be able to implement 
> non-templated overloads for the types you're interested in 
> (without the override keyword, mind you, since they won't be 
> overriding anything).

Hm, I should've thought to try that. I was able to get things 
working as I wanted them to by doing this:

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



More information about the Digitalmars-d-learn mailing list