I wrote a trivial class to test operator overloading - see below.
In Main() I post increment and assign an int.
The assignment happens before the increment - no suprise.
I then perform the equivalent operation on the Test class instance and the assert fails.
Can someone tell me why Cheers.using
System;using
System.Diagnostics;namespace
test_overloading { class Test { private int m_value = 0;static void Main(string[] args) {
int i = 0; int j = i++; //assign then increment Debug.Assert(!i.Equals(j)); Test t1 = new Test(); Test t2 = t1++; //assign then increment Debug.Assert(!t2.Equals(t1));}
public static Test operator++(Test t) {t.m_value++;
return t;}
public bool Equals(object rhs) { if ( !(rhs is Test) ) return false; //if type differs not equal Test t = (Test)rhs; //cast must succeed //objects are equal if m_value members are equal return (m_value == t.m_value);}
}
}

pre vs. post increment and operator overloading
Sniperumm
Hi,
the assignment t1 = new Test() creates an instance of Test and assigns it to the reference t1. The instance is initialised with m_value = 0.
Then you do t1++ (which takes m_value from 0 to 1 in the instance referenced by t1) and then you create a reference t2 which points to the same instance as t1, therefore t2 and t1 are references pointing to the same object whose m_value = 1.
This makes your assertion fail.
Regards.