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



 DEVELOP > c-Plus-Plus > C++ Primer (4th Ed.) - lvalue op= expr

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "India"
Date: 21 Dec 2007 05:03:10 AM
Object: 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
.

User: "Andrew Koenig"

Title: Re: C++ Primer (4th Ed.) - lvalue op= expr 24 Dec 2007 11:29:39 AM
<subramanian100in@yahoo.com> wrote in message
news:5ddc424f-04d9-41be-948e-59180ea8f094@e10g2000prf.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.
.

User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: C++ Primer (4th Ed.) - lvalue op= expr 22 Dec 2007 07:40:47 AM
On 2007-12-21 12:03, subramanian100in@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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER