lvalue not needed for operator += : 5 += obj works

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Dec 22, 2007.

  1. , India

    , India Guest

    Consider the following program:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    class Test
    {
    public:
    Test(int arg = 10);
    int val;

    };

    Test::Test(int arg) : val(arg)
    {
    cout << "one arg ctor called" << endl;

    }

    Test operator+=(Test lhs, Test rhs)
    {
    cout << "from operator+=" << endl;
    return Test(lhs.val + rhs.val);

    }

    int main()
    {
    Test obj;

    5 += obj;

    return EXIT_SUCCESS;

    }

    This program compiles fine with both g++ and VC++ 2005 Express Edition
    and both produced the following output:

    one arg ctor called
    one arg ctor called
    from operator+=
    one arg ctor called

    My question:
    Why doesn't the standard make the '+=', '-=', etc. be defined only as
    non-static member functions similar to the way that an 'operator=,
    operator[], operator(), and operator->' must be non-static member
    function, so that it will disallow 5 += obj; (It should disallow
    because the constant 5 appears on the left hand side)

    Is there any advantage for the operators +=, -=, *=, etc be allowed as
    non-member
    functions ?

    Kindly clarify

    Thanks
    V.Subramanian
    , India, Dec 22, 2007
    #1
    1. Advertising

  2. , India

    Salt_Peter Guest

    Re: lvalue not needed for operator += : 5 += obj works

    On Dec 21, 10:42 pm, ", India"
    <> wrote:
    > Consider the following program:
    >
    > #include <iostream>
    > #include <cstdlib>
    >
    > using namespace std;
    >
    > class Test
    > {
    > public:
    > Test(int arg = 10);
    > int val;
    >
    > };
    >
    > Test::Test(int arg) : val(arg)
    > {
    > cout << "one arg ctor called" << endl;
    >
    > }
    >
    > Test operator+=(Test lhs, Test rhs)
    > {
    > cout << "from operator+=" << endl;
    > return Test(lhs.val + rhs.val);
    >
    > }
    >
    > int main()
    > {
    > Test obj;
    >
    > 5 += obj;
    >
    > return EXIT_SUCCESS;
    >
    > }
    >
    > This program compiles fine with both g++ and VC++ 2005 Express Edition
    > and both produced the following output:
    >
    > one arg ctor called
    > one arg ctor called
    > from operator+=
    > one arg ctor called
    >
    > My question:
    > Why doesn't the standard make the '+=', '-=', etc. be defined only as
    > non-static member functions similar to the way that an 'operator=,
    > operator[], operator(), and operator->' must be non-static member
    > function, so that it will disallow 5 += obj; (It should disallow
    > because the constant 5 appears on the left hand side)
    >
    > Is there any advantage for the operators +=, -=, *=, etc be allowed as
    > non-member
    > functions ?
    >
    > Kindly clarify
    >
    > Thanks
    > V.Subramanian


    Why should it disallow it, if you plan to shoot yourself in the foot,
    its not the standard's concern.
    If you are going to provide non-member operators like +=, its up to
    you to provide something that makes sense.
    You op+= does not use references and it returns a local copy. Why?
    At the very least it should be modifying the lhs value and returning a
    reference to it.
    Interestingly enough, once you do that, the problem solves itself.

    class Test
    {
    int val;
    public:
    Test(int arg = 10) : arg(val) { }
    friend Test& operator+=(Test& lhs, const Test& rhs);
    };

    Test& operator+=(Test& lhs, const Test& rhs)
    {
    std::cout << "from operator+=" << std::endl;
    lhs.val += rhs.val;
    return lhs;
    }

    int main()
    {
    Test obj;
    Test rhs;
    obj += rhs; // obj.val = 20
    // 5 += obj; // no match for op+=
    }
    Salt_Peter, Dec 22, 2007
    #2
    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. Mark Kamoski
    Replies:
    3
    Views:
    15,681
    Jay B. Harlow [MVP - Outlook]
    Aug 9, 2003
  2. alf
    Replies:
    9
    Views:
    402
  3. Kavya
    Replies:
    9
    Views:
    508
    Dik T. Winter
    Oct 28, 2006
  4. Replies:
    11
    Views:
    693
    James Kuyper
    Sep 22, 2008
  5. Julian Mehnle
    Replies:
    0
    Views:
    238
    Julian Mehnle
    Jul 17, 2003
Loading...

Share This Page