Unary operators overloading in C#
Forums:
class Point {
public int x;
public int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public static Point operator ++(Point p) {
return new Point(p.x + 1, p.y + 1);
}
public static Point operator -(Point p) {
return new Point(-p.x, -p.y);
}
}
var p = new Point(1, 1);
p++;
//p3.x is 2 and p3.y is 2
++p;
//p3.x is 3 and p3.y is 3
p = -p;
//p3.x is -3 and p3.y is -3
- Log in to post comments