operator Overloading in C# -
class point { private int m_pointx; private int m_pointy; public point(int x, int y) { m_pointx = x; m_pointy = y; } public static point operator+(point point1, point point2) { point p = new point(); p.x = point1.x + point2.x; p.y = point1.y + point2.y; return p; } }
example:
point p1 = new point(10,20); point p2 = new point(30,40) p1+p2; // operator overloading
- is necessary declare operator overloading function static? reason behind this?
- if want overload + accept expression 2+p2, how this?
- yes. because aren't dealing instances operators.
- just change types want.
here example #2
public static point operator+(int value, point point2) { // logic here. }
you have other way parameters if want p2 + 2
work.
see http://msdn.microsoft.com/en-us/library/8edha89s.aspx more information.
Comments
Post a Comment