operator+

Discussion in 'C++' started by Gaijinco, Oct 30, 2006.

  1. Gaijinco

    Gaijinco Guest

    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?
     
    Gaijinco, Oct 30, 2006
    #1
    1. Advertising

  2. 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.
     
    Jacek Dziedzic, Oct 30, 2006
    #2
    1. Advertising

  3. * 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?
     
    Alf P. Steinbach, Oct 30, 2006
    #3
  4. Gaijinco

    Ron Natalie Guest

    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.
     
    Ron Natalie, Oct 30, 2006
    #4
  5. Gaijinco

    Gaijinco Guest

    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?
     
    Gaijinco, Oct 30, 2006
    #5
  6. Gaijinco

    Nate Barney Guest

    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<int> p;
    int a=1,b=2;

    int c = p(a,b);

    return 0;
    }
     
    Nate Barney, Oct 30, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    617
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    447
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,080
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    5,068
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,752
    Kanenas
    May 30, 2005
Loading...

Share This Page