Beginner problem: casting in opCmp override

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 8 14:42:30 PDT 2013


On Monday, July 08, 2013 23:31:14 Ugbar Ikenaki wrote:
> Also…Rat is a struct, not a class. Herein might lie the problem.

So, this is Rat's opCmp, correct? If it's a struct, it makes no sense for it 
to take an Object. Object is the base class for all classes and has nothing to 
do with structs. opCmp needs to take Rat, not Object. And to be fully 
functional, you might need to have a ref overload as well (since sometimes the 
compiler and runtime get overly picky about exact signatures - which is slowly 
getting fixed fortunately). So, something like this should work:

int opCmp(Rat other)
{
    return this.opCmp(other);
}

int opCmp(ref Rat other)
{
    //do stuff for comparison
}

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list