What is the difference between operator and function?
Basically, why'd we need operator if we have function? Thanks.
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;
Re: What is the difference between operator and function?
Quote:
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.
Re: What is the difference between operator and function?
sizeof isn't the same thing. It's evaluated at compile time, not at runtime.
Re: What is the difference between operator and function?
Quote:
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.