473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

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
Dec 22 '07 #1
1 1258
On Dec 21, 10:42 pm, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
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+=
}
Dec 22 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form...
2
by: Mark Stijnman | last post by:
I would like to be able to have an object accessible as a vector using the operator, but able to track modifications to its data, so that it can update other internal data as needed. I figured...
10
by: Stub | last post by:
The following code gives error C2105: '++' needs l-value. char buf; buf++ = 'A';//buf++ isn't a lvalue Why buf++ isn't a lvalue? Is this according to C Standard or is there some reason for...
24
by: Romeo Colacitti | last post by:
Hi, Does anyone here have a strong understanding for the meanings of the terms "lvalue" and "rvalue" as it pertains to C, objects, and different contexts? If so please share. I've been...
9
by: junky_fellow | last post by:
Consider the following piece of code: (char *)0x100; /* I know that converting an integer to pointer type is implementation defined. But forget this for a moment */ My question is, Why the...
14
by: nobrow | last post by:
Yes I know what lvalue means, but what I want to ask you guys about is what are all valid lvalues ... a *a a *(a + 1) .... What else?
1
by: subramanian100in | last post by:
Consider the following: int x; int y; int z; (x+y) = z; For this statement, I get the following error with g++ compiler: error: non-lvalue in assignment Suppose I have a class Test and x,...
54
by: Rahul | last post by:
Hi Everyone, I have the following piece of code, and i expected an error, however i don't get an error, int main() { int aa=0,b=0; 1>0?aa:b = 10; printf("value %d %d\n",aa,b);
2
by: Chad | last post by:
The following question actually stems from an old Chris Torek post. And I quote from the following old CLC url ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.