Difference between operator and function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Vinokur

    #1

    Difference between operator and function

    What is the difference between an operator and a function in C++?

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com

    http://sourceforge.net/users/alexvn

  • Alf P. Steinbach

    #2
    Re: Difference between operator and function

    * Alex Vinokur:
    What is the difference between an operator and a function in C++?
    An operator is a special kind of function, with a non-alphanumeric name,
    an operator symbol, which can be invoked using prefix, infix or postfix
    notation, e.g.

    -x // prefix unary minus '-'
    a < b // infix less than '<'
    functioid( 3.14 ) // Postfix function call operator '()'

    To define an operator you use the word 'operator' followed by the
    operator symbol.

    E.g.,

    bool operator<( Thing const& a, Thing const& b )
    {
    return a.isLessThan( b );
    }

    There are restrictions on defining some operators.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Jack Klein

      #3
      Re: Difference between operator and function

      On Sun, 29 Oct 2006 18:02:36 +0100, "Alf P. Steinbach"
      <alfps@start.no wrote in comp.lang.c++:
      * Alex Vinokur:
      What is the difference between an operator and a function in C++?
      >
      An operator is a special kind of function, with a non-alphanumeric name,
      an operator symbol, which can be invoked using prefix, infix or postfix
      notation, e.g.
      The concept of "special kind of function" is a very strange one, and
      certainly not supported by any wording in the C++ language.

      The notion that operators must have a non-alphanumeric name is quite
      easily refuted by the following operators:

      - sizeof
      - const_cast
      - static_cast
      - dynamic_cast
      - reinterpret_cas t

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://c-faq.com/
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      • Jack Klein

        #4
        Re: Difference between operator and function

        On 29 Oct 2006 08:50:48 -0800, "Alex Vinokur"
        <alexvn@users.s ourceforge.netw rote in comp.lang.c++:
        What is the difference between an operator and a function in C++?
        >
        Alex Vinokur
        email: alex DOT vinokur AT gmail DOT com

        http://sourceforge.net/users/alexvn
        I read, and seriously disagree with, Alf's reply.

        Here is the difference as I see it:

        Operators are defined as operators by the language. Some operators
        are overloadable, others are not. Most operators do not create
        sequence points (although when overloaded, the overloaded versions are
        functions and do impose sequence points). The number of operands on
        which an operator operates is fixed and immutable.

        Other than the fact that functions are overloadable, none of the above
        applies to them.

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://c-faq.com/
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        • Alf P. Steinbach

          #5
          Re: Difference between operator and function

          * Jack Klein:
          On Sun, 29 Oct 2006 18:02:36 +0100, "Alf P. Steinbach"
          <alfps@start.no wrote in comp.lang.c++:
          >
          >* Alex Vinokur:
          >>What is the difference between an operator and a function in C++?
          >An operator is a special kind of function, with a non-alphanumeric name,
          >an operator symbol, which can be invoked using prefix, infix or postfix
          >notation, e.g.
          >
          The concept of "special kind of function" is a very strange one,
          No, it's not a strange concept, it's the basic idea.

          The standard calls it an "operator function" (some operator functions
          can be user-defined).

          and
          certainly not supported by any wording in the C++ language.
          Yes, the standard does not AFAIK define the general concept of
          operators. AFAIK there's not even a list of operators, except if a
          table with punctuation symbols thrown in is OK. The reader is simply
          supposed to already have a working notion of what operators are.

          The notion that operators must have a non-alphanumeric name is quite
          easily refuted by the following operators:
          >
          - sizeof
          - const_cast
          - static_cast
          - dynamic_cast
          - reinterpret_cas t
          In addition to those there's 'new', 'delete', 'and', 'and_eq', 'bitand',
          'bitor', 'compl', 'not', 'not_eq', 'or', 'or_eq', 'xor' and 'xor_eq'.

          So clearly an operator mustn't necessarily have a non-alphanumeric name.

          But operators /usually/ are non-alphanumeric (those I listed here,
          except the two first, are in practice not used, and those you listed
          cannot be overridden, so these alphanumeric operators are very special
          cases), and since they usually are, they /can/ be non-alphanumeric.

          --
          A: Because it messes up the order in which people normally read text.
          Q: Why is it such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          • Alex Vinokur

            #6
            Re: Difference between operator and function

            "Jack Klein" <jackklein@spam cop.netwrote in message news:nfqak2hel8 ifn1d8bh2otutfh stc0jij7b@4ax.c om...
            [snip]
            Most operators do not create sequence points (although when overloaded, the overloaded versions are functions and do impose
            sequence points).
            [snip]

            What does "sequence points" mean?

            --
            Alex Vinokur
            email: alex DOT vinokur AT gmail DOT com

            http://sourceforge.net/users/alexvn




            Comment

            • Victor Bazarov

              #7
              Re: Difference between operator and function

              Alex Vinokur wrote:
              "Jack Klein" <jackklein@spam cop.netwrote in message
              news:nfqak2hel8 ifn1d8bh2otutfh stc0jij7b@4ax.c om... [snip]
              >Most operators do not create sequence points (although when
              >overloaded, the overloaded versions are functions and do impose
              sequence points).
              [snip]
              >
              What does "sequence points" mean?
              www.Google.com and make it a habit.


              Comment

              • Marcus Kwok

                #8
                Re: Difference between operator and function

                Alex Vinokur <alexvn@users.s ourceforge.netw rote:
                "Jack Klein" <jackklein@spam cop.netwrote in message news:nfqak2hel8 ifn1d8bh2otutfh stc0jij7b@4ax.c om...
                [snip]
                >Most operators do not create sequence points (although when overloaded, the overloaded versions are functions and do impose
                sequence points).
                [snip]
                >
                What does "sequence points" mean?
                Since this is something that C++ inherited from C:


                --
                Marcus Kwok
                Replace 'invalid' with 'net' to reply

                Comment

                Working...