[help]operator overloading with opEquals in a class

Benjamin Thaut code at benjamin-thaut.de
Wed Nov 3 10:57:09 PDT 2010


Am 03.11.2010 13:07, schrieb zhang:
> This code belown can be compiled with DMD 2.050. However, it throws an error message:
> core.exception.HiddenFuncError: Hidden method called for main.AClass
>
> I'm not sure about whether it is a bug. Thanks for any help.
>
>
>
> import std.stdio;
>
> class AClass
> {
>      int data;
>
> // It's wrong
>      bool opEquals(AClass a)
>      {
>          writefln("running here.");
>          return data == a.data;
>      }
>
> // It works
> //    bool opEquals(Object a)
> //    {
> //        writefln("running here.");
> //        return data == (cast(AClass)a).data;
> //    }
>
> }
>
>
> int main(string[] args)
> {
>
>      AClass class1 = new AClass();
>      AClass class2 = new AClass();
>
>      if(class1 == class2)
>          writefln("==");
>      else
>          writefln("!=");
>
>      return 0;
> }
>
> ----------
> Zhang<bitworld at qq.com>
>
> s

I'm not shure if it is a bug or not, but to fix it you could add "alias 
object.opEquals opEquals;" before your first opEquals. Had that already 
many times, it also happens a lot with opCmp.

-- 
Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list