473,386 Members | 1,602 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,386 software developers and data experts.

C++ Primer (4th Ed.) - lvalue op= expr

In C++ Primer(4th Edition) by Stanley Lippman, in page 162, the
following is mentioned:

The general syntactic form of a compound assignment operator is

a op= b

where op= may be one of the following ten operators:
+= -= *= /= %= // arithmetic operators
<<= >>= &= ^= != // bitwise operators
In page 512 in this book, in the chapter 14 on Overloaded Operations
and Conversions, the following is mentioned:

Like assignment, the compound-assignment operators ordinarily ought to
be members of the class. Unlike assignment, they are not required to
be so and the compiler will not complain if a non-member compound-
assignment operator is defined.

I tried the following sample 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;

Is there any advantage for the compound-assignment operators(as per
the definition given in the beginning above) be allowed as non-member
functions ?

Kindly clarify

Thanks
V.Subramanian
Dec 21 '07 #1
2 1758
On 2007-12-21 12:03, su**************@yahoo.com, India wrote:
In C++ Primer(4th Edition) by Stanley Lippman, in page 162, the
following is mentioned:

The general syntactic form of a compound assignment operator is

a op= b

where op= may be one of the following ten operators:
+= -= *= /= %= // arithmetic operators
<<= >>= &= ^= != // bitwise operators
In page 512 in this book, in the chapter 14 on Overloaded Operations
and Conversions, the following is mentioned:

Like assignment, the compound-assignment operators ordinarily ought to
be members of the class. Unlike assignment, they are not required to
be so and the compiler will not complain if a non-member compound-
assignment operator is defined.

I tried the following sample 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;

Is there any advantage for the compound-assignment operators(as per
the definition given in the beginning above) be allowed as non-member
functions ?
Yes, by allowing the compound assignment operators to be non-members the
standard allows usages such as 5 += obj. If you can not come up with a
good reason why it should not be allowed then it probably should be
allowed, just because you (and probably almost everyone else) might not
see any good uses for it does not mean that there are none.

--
Erik Wikström
Dec 22 '07 #2
<su**************@yahoo.comwrote in message
news:5d**********************************@e10g2000 prf.googlegroups.com...
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;
Because there's no need.

Plain assignment has to be a member because the compiler generates it if you
don't define it; and unless it's a member, there is no reliable way for the
compiler to know that you didn't define it. +=, etc. are never generated
automatically, so the compiler doesn't care what you do with them.
Dec 24 '07 #3

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...
14
by: Michael Ovetsky | last post by:
Consider: int g(){int b=1; return b;} int main() { int d=2; g()=d; }
5
by: hajime | last post by:
I purchased this book: C++ Primer, 4th Edition ISBN: 0201721481 in Australia. The right side of the last three lines on page 231 are not legible. A stain shows a piece of paper was on that part...
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: perry.yuan | last post by:
Hi there, I got a problem: how to return an lvalue from conditional expression in C programming language. Any solution or suggestion to my problem in either C or C++ is welcome. On the CPU...
7
by: Lycan. Mao.. | last post by:
Hello, I am a newbie in C++ and I'm in trouble in choosing books, I hope some one who can give me some tips. I'm already know C and a little about Scheme, C#, Python, Lua and so on, and now I want...
1
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
1
by: mmdspam | last post by:
Hi, Can someone please tell me the answer to exercise 1.20 in C++ Primer 4th edition. I am stuck on this question. Thank you in advance
2
by: jerry | last post by:
can anyone tell me where can i get the keys to c++ primer 4th edition, pdf or chm type are both ok. thank you!
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.