Operators overloading..question..

FlyBoy

Centurion
Joined
Sep 6, 2004
Messages
106
yep..thats right,me again..i hope that im not making you nervous or something with my question,im just a c# newbie :o (for now...hehe :) ).
this time,i have a question regarded to "Operators overloading".
i've snooped abit at this code (from MSDN..) :
Code:
using System;

public struct Complex 
{
   public int real;
   public int imaginary;

   public Complex(int real, int imaginary) 
   {
      this.real = real;
      this.imaginary = imaginary;
   }

   // Declare which operator to overload (+), the types 
   // that can be added (two Complex objects), and the 
   // return type (Complex):
   public static Complex operator +(Complex c1, Complex c2) 
   {
      return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
   }
   // Override the ToString method to display an complex number in the suitable format:
   public override string ToString()
   {
      return(String.Format("{0} + {1}i", real, imaginary));
   }

   public static void Main() 
   {
      Complex num1 = new Complex(2,3);
      Complex num2 = new Complex(3,4);

      // Add two Complex objects (num1 and num2) through the
      // overloaded plus operator:
      Complex sum = num1 + num2;

     // Print the numbers and the sum using the overriden ToString method:
      Console.WriteLine("First complex number:  {0}",num1);
      Console.WriteLine("Second complex number: {0}",num2);
      Console.WriteLine("The sum of the two numbers: {0}",sum);
 
   }
}

my question is as follows..: in all the example i've seen so far about OO (operators overloading...) i've seen that the overloading method (...or whatever it called) used parameters of the costructor type...hmm..is this necessery?
i really (yet..) dont understad it that good....
10x in advance.
 
The types that you specify for an operator are the types you want to cover. If it makes sense that your type work with the base types, such as int, then you would have to write an operator overload to use both types: yours and int.

For example:
C#:
   public static Complex operator +(Complex lhs, int rhs)
   {
      return new Complex(lhs.real + rhs, lhs.imaginary);
   }

This says allow adding a Complex type with an int, only like this:
C#:
Complex a = new Complex(1, 2);
int b = 6;
Complex c = a + b;

If you tried "Complex d = b + a;" it wouldn't compile as you didn't define an operator for a Complex type on the right, with an int on the left.

Some books like to use lhs and rhs for each param in an operator overload to help spell out "left hand side" and "right hand side".

-ner
 
Nerseus said:
The types that you specify for an operator are the types you want to cover. If it makes sense that your type work with the base types, such as int, then you would have to write an operator overload to use both types: yours and int.

For example:
C#:
   public static Complex operator +(Complex lhs, int rhs)
   {
      return new Complex(lhs.real + rhs, lhs.imaginary);
   }

This says allow adding a Complex type with an int, only like this:
C#:
Complex a = new Complex(1, 2);
int b = 6;
Complex c = a + b;

If you tried "Complex d = b + a;" it wouldn't compile as you didn't define an operator for a Complex type on the right, with an int on the left.

Some books like to use lhs and rhs for each param in an operator overload to help spell out "left hand side" and "right hand side".

-ner
GOT IT! thanks...(saved it to doc..like every reply i get..(-: )
 
Back
Top