opEquals footprint

Unknown W. Brackets unknown at simplemachines.org
Wed Mar 22 07:43:29 PST 2006


class A
{
	int opEquals(Object o)
	{
		A a = cast(A) o;

		if (a is null)
			return false;
		else
			return this.member1 == a.member1;
	}
}

Casting will return null if it fails, so to speak (so you definitely 
need to check after it for null.)  Typeof will always give "Object" for 
o since that's its compile-time type.

-[Unknown]


> xs0 wrote:
>> int opEquals(Object)
>>
>> so it overrides the default implementation found in, you guessed it, 
>> Object :) Same goes for opCmp, I think..
>>
>>
>> xs0
> 
> So what's the best way to actually check the type in the opEquals/opCmp 
> method?  Something like...
> 
> class A
> {
>   int opEquals(Object o)
>   {
>     // check for null
>     if(o is null)
>       return false;
>     // check type
>     if(typeid(typeof(o)) != typeid(A))
>       return false;
>     // cast
>     A that = cast(A) o;
>     // actually check fields
>     return this.a == that.a && this.b == that.b && ...;
>   }
> }
> 
> Or is there a better way?  asserts?
> 
> How do you hard-core D programmers usually do it?
> 
> Cheers,
> Erik



More information about the Digitalmars-d-learn mailing list