www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opEquals

reply "deed" <none none.none> writes:
interface I
{
     bool opEquals(I i);
}

class C : I
{
     bool opEquals(I i)
     {
         return true;
     }
}

void main()
{
     I i1 = new C;
     I i2 = new C;
     assert(i1 == i2);  // Assertino failure
     assert(i1 != i2);  // Passes, although it's the opposite of 
what I want..
}

What's missing?
Dec 04 2012
parent "Dan" <dbdavidson yahoo.com> writes:
On Wednesday, 5 December 2012 at 00:17:05 UTC, deed wrote:
 interface I
 {
     bool opEquals(I i);
 }

 class C : I
 {
     bool opEquals(I i)
     {
         return true;
     }
 }

 void main()
 {
     I i1 = new C;
     I i2 = new C;
     assert(i1 == i2);  // Assertino failure
     assert(i1 != i2);  // Passes, although it's the opposite of 
 what I want..
 }

 What's missing?

It looks like you want the opEquals to be called and it is not. Match the signature of it to what is in object.di. This may be what you want. ---------------------------------- import std.stdio; interface I { bool opEquals(Object i); } class C : I { override bool opEquals(Object i) const { writeln("Called"); return true; } } void main() { I i1 = new C; I i2 = new C; writeln(i1 == i2); writeln(i1 != i2); }
Dec 05 2012