Operator overloading

Reply

Join Date: Oct 2009
Posts: 2
Reputation: sumit21amig is an unknown quantity at this point 
Solved Threads: 0
sumit21amig sumit21amig is offline Offline
Newbie Poster

Operator overloading

 
0
  #1
Oct 26th, 2009
How can i maintain operator's precedence order while overloading "+" and "*" in a class? e.g. I want same result on the calculation of following objects:
d=a+b*c;
d=c*a+b;
Please help!

Also tell me why simple constructor is able to call static data in a class?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,275
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 354
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Nearly a Posting Maven
 
0
  #2
Oct 26th, 2009
Hi sumit21amig, welcome!
Don't know what you are getting at here. * ALWAYS takes precedence over +. That is just standard math. The formulas you give will only have the same result if a,b and c have the same value or some of them are zero.
Static data are always available even to a simple constructor.
Last edited by ddanbe; Oct 26th, 2009 at 10:35 am.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: sumit21amig is an unknown quantity at this point 
Solved Threads: 0
sumit21amig sumit21amig is offline Offline
Newbie Poster
 
0
  #3
Oct 26th, 2009
of course we have BODMAS rule of arithmetic which is quite applicable with integer, float, double and etc but the behavior of arithmetic operators into the context of class/object is defined by programmer to the compiler.
So, how this precedence/BODMAS is applied in a class?
I hope i am clear.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,275
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 354
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Nearly a Posting Maven
 
0
  #4
Oct 26th, 2009
Most of the time you don't have to bother about operator overloading in C#. The compiler is even smart enough to figure out that a string + an integer ends in a string with the number appended. The C# compiler, like any other compiler I ever used always used the BODMAS rule as you call it. If you want to change the order of calculation use brackets.
But if the expressions d=a+b*c; and d=c*a+b; always have to resolve to the same value you have to design your own math I guess.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 63
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 17
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
1
  #5
Oct 26th, 2009
According to some quick google searches (google makes me look so smart!) in "ECMA-334: 14.2.2 Operator overloading" there is a line that reads "User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator." So, if I understand that correctly (I'm not done with my Red Bull, so anyone out there with knowledge please reply) you can't change the precedence of operators. For a fun test, derive a class from Integer and overload * and + by swapping them (make * actually add, and + multiply), then try to solve
  1. MyInteger two = 2, three = 3, four = 4; MyInteger X = two * three + four;
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 672
Reputation: Ryshad is a jewel in the rough Ryshad is a jewel in the rough Ryshad is a jewel in the rough Ryshad is a jewel in the rough 
Solved Threads: 139
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Practically a Master Poster
 
0
  #6
Oct 27th, 2009
Originally Posted by sumit21amig View Post
How can i maintain operator's precedence order while overloading "+" and "*" in a class? e.g. I want same result on the calculation of following objects:
d=a+b*c;
d=c*a+b;
Please help!

Also tell me why simple constructor is able to call static data in a class?
As ddanbe pointed out, multiplication takes precedance so your equations are calculated as:

d = a + (b*c);
d = (c*a) + b;

You would need to enclose parts in brackets if you want them to be calculated prior to the multiplication:
d = (a + b) * c;
d = c * (a + b);
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Reply

Tags
c#, operator

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 766 | Replies: 5
Thread Tools Search this Thread



Tag cloud for c#, operator
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC