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

S

subramanian100in

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
 
S

Salt_Peter

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+=
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top