A fresh look at comparisons

Janice Caron caron800 at googlemail.com
Tue Apr 15 04:33:14 PDT 2008


On 15/04/2008, Georg Wrede <georg at nospam.org> wrote:
>  Well, that depends. If I design a class hierarchy where I want to compare
> instances, I'd either have the superclass define the comparison, if it's
> appropriate. If, OTOH, the different subclasses should compare differently,
> then of course the comparison has to be defined for them separately. I'm not
> sure I see a big problem here.

The only thing that would be different is the *default* behavior, by
which I mean the behavior of a subclass which does not explicitly
provide its own equality comparison.

Under the inheritance mechanism, the default comparison function
returns true iff super.opEquals(Object other) returned true.

Under the is(==) mechanism, the default comparison function would
return true iff all of the following conditions are simultaneously
met: (1) the lhs and rhs were of the same type, (2) lhs.super compares
equal to rhs.super, and (3) all member variables of lhs compare equal
to corresponding member variables of rhs.

So, if you subclass A to get B, but don't add any new member
variables, then the default comparison test would be the same. But if
you add new member variables, then those would get included, by
default, in the new test.

But again, I'm only talking about /default/ behavior here. If you
don't want default behavior, you can override, and get any behavior
you want.



More information about the Digitalmars-d mailing list