CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    What is the difference between operator and function?

    Basically, why'd we need operator if we have function? Thanks.

  2. #2
    Join Date
    Aug 2007
    Posts
    858

    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.

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: What is the difference between operator and function?

    Quote Originally Posted by Speedo View Post
    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.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What is the difference between operator and function?

    sizeof isn't the same thing. It's evaluated at compile time, not at runtime.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: What is the difference between operator and function?

    Quote Originally Posted by Speedo View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured