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 
  1. is necessary declare operator overloading function static? reason behind this?
  2. if want overload + accept expression 2+p2, how this?

  1. yes. because aren't dealing instances operators.
  2. 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

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -