Why does this opCmp function not work
Simen Kjærås
simen.kjaras at gmail.com
Wed Sep 23 11:28:11 UTC 2020
On Wednesday, 23 September 2020 at 11:19:15 UTC, Ruby The
Roobster wrote:
> bool opEquals(shape rhs)
> {
> return(rhs.volume == this.volume);
> }
This is a wrong implementation of opEquals for a class. The
documentation mentions
(https://dlang.org/spec/operatoroverloading.html#equals):
> 4. If overridding Object.opEquals() for classes, the class
> member function signature should look like:
>
> class C
> {
> override bool opEquals(Object o) { ... }
> }
A correct implementation would be:
override bool opEquals(Object o) {
if (!cast(Shape)o) return false;
return (cast(Shape)o).volume == this.volume;
}
--
Simen
More information about the Digitalmars-d
mailing list