this compare not using opCmp?

Era Scarecrow rtcvb32 at yahoo.com
Sun Apr 29 22:09:46 PDT 2012


  In some code I'm working on, my asserted when I've confirmed it 
was correct. With the opCmp() overridden.  'this' refers to the 
current object, so why doesn't the first one succeed?

class X {
   int z;

   this(int xx) {
	z = xx;
   }

   override int opCmp(Object y){
	X x = cast(X) y;
	return z - x.z;
   }

   void something(X rhs) {
     assert(this.opCmp(rhs) == 0); //works fine.
     assert(this == rhs); 	  //fails?
   }
}

int main(){
	X a = new X(10);
	X b = new X(10); //different objects, same compare value
	a.something(b);
	return 0;
}


More information about the Digitalmars-d-learn mailing list