yarex said:
(x == y || x == z) is wrong.
x == (y || z) .... when x ==0 and y ==1 and z==0
0 == (1||0) .... = (0==1) = 0. so it gives false
but when its (x == y || x == z)
(0 == 1 || 0==0) ....... (0 || 1) = 1
So these two are not equal in case if x=0 and y=1 and z=0
or case
x=0 and y=0 and x=1
(other cases are ok
)
Yes, but in his case, the semantics of
is what he was looking for, just in a shorter way of doing it, so the semantics that you pointed out, while valid in certain situations, was not what he was looking for.
Now, back to the topic, you
can doing something like
But not normally and not all too pretty (nor without abusing the privilege of overloading operators). I felt like screwing with C#'s overloading of operators and wanted to see if you could abuse it in somewhat the same way as in C++, and I'm happy to say that you can, to a degree.
Code:
class TestClass
{
public TestClass ( int i1, int i2 )
{
x = i1;
y = i2;
}
public static bool operator == ( int i, TestClass tc )
{
return (i == tc.x || i == tc.y);
}
public static bool operator != ( int i, TestClass tc )
{
return !(i == tc);
}
int x
, y;
}
class TestClass2
{
public TestClass2 ( int i )
{
x = i;
}
public static TestClass operator | ( TestClass2 tc1, TestClass2 tc2 )
{
return new TestClass( tc1.x, tc2.x );
}
public int x;
}
Using these two classes together, it's possible for me to write something of the form
Code:
int i = 20;
TestClass2 tc1 = new TestClass2(10)
, tc2 = new TestClass2(20);
i == (tc1 | tc2);
and have it be semantically equivalent to
Please note that I do not advocate doing this, as it is indeed a bit of a misuse of the powerful and easily abusable operator overloading concept.
However, do note that I love that you can do this.