| | |
Operator overloading
![]() |
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.
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
Make love, no war. Cave ab homine unius libri.
Danny
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.
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
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Oct 2009
Posts: 63
Reputation:
Solved Threads: 17
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
C# Syntax (Toggle Plain Text)
MyInteger two = 2, three = 3, four = 4; MyInteger X = two * three + four;
0
#6 Oct 27th, 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?
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
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- a small prob with operator overloading (C++)
- Operator Overloading HELP! (C++)
- about operator overloading (C++)
- operator overloading (Java)
- Operator Overloading Question (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- program for finding factorial of a number using *operator overloading (C++)
Other Threads in the C# Forum
- Previous Thread: Need help with complicated nest if statements :S
- Next Thread: Calling a methond
Views: 766 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for c#, operator
& .net 2d 2008 access ajax application array asp asp.net association automation avltree buttons c# chat class complex database datadirectory datagrid datagridview delegate desktop developer development directshow display dll drawing effects efficient excel file flash form forms gdi+ graphics image inline instance java javascript list listbox login match math media microsoft multilingual mysql notepad odp.net operator oracle outlook2007 pdf photoshop php picturebox player problem programming reflection remote remoting resource saving search seralization server socket software sql sql-server storing stream string text textbox timer treeview url variable vb.net vc++ video visual visualstudio web webbrowser windows winforms word wpf wpfc# write xml







