Share via


operator overloading vs operator overriding

Question

Monday, October 2, 2006 4:45 PM

what is the difference between operator overloading and operator overriding

thanks in advance for ur replies

All replies (8)

Monday, October 2, 2006 5:06 PM âś…Answered

Operator overloading:

http://www.c-sharpcorner.com/Language/OperatorOverloadingPSD.asp

http://msdn.microsoft.com/library/en-us/csref/html/vcwlkOperatorOverloadingTutorial.asp?frame=truehttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_7_2_2.asp

 

operator overriding basically overrides the operators - you change the behavior the way the operators work.

http://www.markberck.nl/article_21d48689-9cdd-4763-8c57-79d93b5e1fea.aspx

http://builder.com.com/5100-6373-1050958.html

http://msdn2.microsoft.com/en-us/library/ms173147.aspx

 

I hope these resources will help you

 

edit: looks like TML beat me to it :-) Good explanation too


Monday, October 2, 2006 5:03 PM

Overloading vs. overriding is the same, in general, as it is for methods.  Overriding is the process of redefining a method (or operator) in a derived class with the same signature as a member in the base class but performing different work.  You use overriding all the time.  For example when you create a form in a WinForms app you normally override the base Form.OnLoad method with your own. 

Overloading is the process of defining several methods (or operators) with the same name but different signatures (a signature being the method name and its arguments).  Whenever you type an overloaded method's name into the IDE you'll get the Intellisense kicking in.  Whenever it shows the "1 of X" information it is identifying the different overloads of the method.  For example String.Format is overloaded.

Now to apply this to operators.  Overriding occurs when you override the base implementation.  For example in almost all cases you should override the equals operator in a structure.  You normally override the not-equals operator as well.

Overloading an operator is normally done for mathematical operators such as + and -.  You would overload these methods to allow you to specify different types to add together.  For example you might specify that your class provides overloads such that you can add two ints, two doubles, two decimals, a decimal and double, a decimal and int, etc.  You can overload non-math operators as well but this isn't as common.  A common technique that you'll see old C++ developers use is to overload the assignment operator to accept multiple types.

Michael Taylor - 10/2/06


Monday, October 2, 2006 5:58 PM

thanks guys  for ur replies

so i assume that both r doing the same thing


Monday, October 2, 2006 5:58 PM

thanks for ur replies


Monday, October 2, 2006 6:01 PM

No they are distinct things.  Overriding changes the behavior defined in a base class.  Overloading provides multiple ways of doing something.  Overloading does not require a base class.

Michael Taylor - 10/2/06


Monday, October 2, 2006 6:05 PM

thanks

ok. so is it the same for  operator overloading & overriding?


Monday, October 2, 2006 6:18 PM

yes i got it Michael Taylor

thanks for ur help

so operator overloading is having the same operator doing different tasks & operator overriding is changing the defined task of a particular operator

is it correct?


Monday, October 2, 2006 6:26 PM

No they are distinct operations irrelevant of whether they are methods, properties or operators.  Only instance methods that are declared virtual can be overridden.  Any method, instance or shared/virtual or non-virtual, can be overloaded.

Perhaps an example will clear things up:

struct Complex
{
   //Overload constructors
   public Complex ( float real )
   {
      m_Real = real; m_Imaginary = 0;
   }
   public Complex ( float real, float imaginary )
   {
      m_Real = real; m_Imaginary = imaginary;
   }

   private float m_Real;
   private float m_Imaginary;
   public float Real
   { get { return m_Real; } }

   public float Imaginary
   { get { return m_Imaginary; } }

   //Override Object.ToString()
   public override string ToString ( )
   { return String.Format("{0} + {1}i", Real, Imaginary); }

   //Override ValueType.Equals()
   public override bool Equals ( object obj )
   {
      Complex cmplx = (Complex)obj;
      return Real == cmplx.Real && Imaginary == cmplx.Imaginary;
   }

   //Override ValueType.GetHashCode()
   public override int GetHashCode ( )
   { return Real.GetHashCode() | Imaginary.GetHashCode(); }

   //Overload equality
   public static bool operator== ( Complex lhs, Complex rhs )
   { return lhs.Equals(rhs); }
   public static bool operator == ( Complex lhs, float rhs )
   { return lhs.Equals(new Complex(rhs)); }

   public static bool operator!= ( Complex lhs, Complex rhs )
   { return !lhs.Equals(rhs); }
   public static bool operator != ( Complex lhs, float rhs )
   { return !lhs.Equals(new Complex(rhs)); }

   //Overload addition
   public static Complex operator+ ( Complex obj1, Complex obj2 )
   { return new Complex(obj1.Real + obj2.Real, obj1.Imaginary + obj2.Imaginary); }
   public static Complex operator + ( Complex obj, float real )
   { return new Complex(obj.Real + real, obj.Imaginary); }

   //Overload subtraction
   public static Complex operator - ( Complex obj1, Complex obj2 )
   { return new Complex(obj1.Real - obj2.Real, obj1.Imaginary - obj2.Imaginary); }
   public static Complex operator - ( Complex obj, float real )
   { return new Complex(obj.Real - real, obj.Imaginary); }
}

In the above code the override keyword identifies overriding.  Technically none of the operators can be overridden in C# because they are all static members (except indexers) but it is still refered to as overriding when you override the default implementation of equality and inequality (which are, by definition, supplied by the base class). 

As you can see with the equality operator we both override the base implementation (even though it is static, this is a special case for compilers) and provided a couple of overloads.  For addition and subtraction we provided a few overloads.  There is no overriding here because the base class does not define these operators.  We also overloaded a few of the standard methods.

Michael Taylor - 10/2/06