Why does Object.opEquals return int

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Mon Nov 13 12:57:01 PST 2006


Bill Baxter wrote:
> Frits van Bommel wrote:
>> Bill Baxter wrote:
>>
>>>                 Also I you're free to make your own classes return 
>>> bool from opCmp, it's just Object opCmp that returns int.
>>
>>
>> [You mean opEquals, of course]
>>
>> Maybe you forgot that a "bool opEquals" doesn't override 
>> Object.opEquals :( .
>> This means that any object ever compared as [1], well, an Object will 
>> still need to define "int opEquals" or run into a potentially 
>> hard-to-spot bug.
>>
>>
>> [1]: or /to/, if your "bool opEquals"'s parameter isn't an Object but 
>> something else (such as a more derived type, typically that of your 
>> class).
> 
> Yes, I thought that it was uncommon for derived classes to be concerned 
> with comparing against base Objects by value.

Not for classes to be used as an AA key. From 
http://www.digitalmars.com/d/arrays.html#associative :
-----
*Using Classes as the KeyType*

Classes can be used as the KeyType. For this to work, the class 
definition must override the following member functions of class Object:

     * hash_t toHash()
     * int opEquals(Object)
     * int opCmp(Object)

Note that the parameter to opCmp and opEquals is of type Object, not the 
type of the class in which it is defined.
-----



More information about the Digitalmars-d mailing list