operator+

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

    #1

    operator+

    I was perfectly sure that this code had to work:

    #include <iostream>

    int main()
    {
    std::cout << operator+(3,4);
    return 0;
    }

    If I can overload the operator+() it means that there is a base
    function defined by the language, right?

  • Jacek Dziedzic

    #2
    Re: operator+

    Gaijinco wrote:
    I was perfectly sure that this code had to work:
    >
    #include <iostream>
    >
    int main()
    {
    std::cout << operator+(3,4);
    return 0;
    }
    >
    If I can overload the operator+() it means that there is a base
    function defined by the language, right?
    Note that you _can't_ overload operator+() for int.

    - J.

    Comment

    • Alf P. Steinbach

      #3
      Re: operator+

      * Gaijinco:
      I was perfectly sure that this code had to work:
      >
      #include <iostream>
      >
      int main()
      {
      std::cout << operator+(3,4);
      return 0;
      }
      Nope.

      If I can overload the operator+() it means that there is a base
      function defined by the language, right?
      No.

      You can overload operator+ for enum and class type arguments, and for
      enum or class type reference arguments, but then there's no built-in
      implementation. I.e. there's no "base" function you can base your
      implementation on. Note for enums: in the case of enum arguments to +,
      without a user-defined operator+ for that type, the arguments are
      promoted to int or higher and the + operator for that integer type is used.

      You can not overload operators for built-in types such as int, nor for
      pointer types.

      --
      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

      • Ron Natalie

        #4
        Re: operator+

        Jacek Dziedzic wrote:
        Gaijinco wrote:
        >I was perfectly sure that this code had to work:
        >>
        >#include <iostream>
        >>
        >int main()
        >{
        > std::cout << operator+(3,4);
        > return 0;
        >}
        >>
        >If I can overload the operator+() it means that there is a base
        >function defined by the language, right?
        >
        Note that you _can't_ overload operator+() for int.
        >
        Further, you can't get at the builtin operator implementations
        by trying to access them as overload functions. It's primarily
        for this reason that funky little template functions like "less"
        exist.

        Comment

        • Gaijinco

          #5
          Re: operator+

          I always assumed that when I right in a code

          a + b

          What really happened is that the language rewrote the code to

          operator+(a,b)

          and that because of it, it was possible to use the function notation
          for all operators.

          So is there anyway in which I can use the function notation instead of
          the classic notation?

          Comment

          • Nate Barney

            #6
            Re: operator+

            Gaijinco wrote:
            I always assumed that when I right in a code
            >
            a + b
            >
            What really happened is that the language rewrote the code to
            >
            operator+(a,b)
            >
            and that because of it, it was possible to use the function notation
            for all operators.
            >
            So is there anyway in which I can use the function notation instead of
            the classic notation?
            Like Ron suggested, use STL functors. In this case, something like this
            might work:

            #include <functional>

            int main()
            {
            std::plus<intp;
            int a=1,b=2;

            int c = p(a,b);

            return 0;
            }

            Comment

            Working...