|
-
March 5th, 2011, 12:35 PM
#1
What is the difference between operator and function?
Basically, why'd we need operator if we have function? Thanks.
-
March 5th, 2011, 01:35 PM
#2
Re: What is the difference between operator and function?
Because people tend to prefer looking at "A = B + C;" instead of "A = Add(B, C);".
Operator overloads are syntactic sugar - they make code look neater/prettier. That's all.
Edit: It may not seem like operators are that useful with a simple example like the above. But compare the following,
Code:
A = Add(B, Add(C, Add(D, Add(E, Add(F, Add(G, H))))));
vs
A = B + C + D + E + F + G + H;
Last edited by Speedo; March 5th, 2011 at 01:37 PM.
-
March 5th, 2011, 02:12 PM
#3
Re: What is the difference between operator and function?
 Originally Posted by Speedo
Because people tend to prefer looking at "A = B + C;" instead of "A = Add(B, C);".
Operator overloads are syntactic sugar - they make code look neater/prettier. That's all.
Edit: It may not seem like operators are that useful with a simple example like the above. But compare the following,
Code:
A = Add(B, Add(C, Add(D, Add(E, Add(F, Add(G, H))))));
vs
A = B + C + D + E + F + G + H;
But operator is not just syntactic sugar. For example operator sizeof doesn't look like syntactic sugar at all. Thanks.
-
March 5th, 2011, 04:03 PM
#4
Re: What is the difference between operator and function?
sizeof isn't the same thing. It's evaluated at compile time, not at runtime.
-
March 6th, 2011, 02:15 AM
#5
Re: What is the difference between operator and function?
 Originally Posted by Speedo
Because people tend to prefer looking at "A = B + C;" instead of "A = Add(B, C);".
Operator overloads are syntactic sugar - they make code look neater/prettier. That's all.
Edit: It may not seem like operators are that useful with a simple example like the above. But compare the following,
Code:
A = Add(B, Add(C, Add(D, Add(E, Add(F, Add(G, H))))));
vs
A = B + C + D + E + F + G + H;
Note that the two examples aren't exactly the same. In the first the evaluation order is determined by the programmer and in the second by the compiler. To be the same the second would have to look like this,
A = B + (C + (D + (E + (F + (G + H)))));
This challenges the notion that operator overloading is mere syntactic sugar for functions. Overloaded operators become part of a syntactic unit where functions aren't equal members. In this sense overloaded operators are more than functions.
Last edited by nuzzle; March 6th, 2011 at 04:04 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|