By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,420 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,420 IT Pros & Developers. It's quick & easy.

how to write an operator

P: n/a
which is preferable and WHY?

class Complex {
....
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);

OR either one but a member operator instead of a friend.

Thanx,
Martin
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Martin Vorbrodt wrote:
which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs); ^
That const adds no value, and there are those who claim it interferes with
certain type-specific operations.

If you meant 'Complex const &', don't do that either, because functions
generally should not return value-types by reference.
OR either one but a member operator instead of a friend.


A member operator might not balance properly:

5 + Complex(8, 2);

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2

P: n/a
>That const adds no value

Well, if you believe what some people say about operator overloading and "do as
the ints do" then it does make it behave more like a function that returns a
built-in type by value.

int a,b,c;
(a+b) = c; //This should not compile

Complex a,b,c;
(a+b) = c; //This will compile if return value is not const.

For reasons I have never grasped a user-defined type returned by value is not
an lvalue yet it can be the target of modifying operations. To make the
overloaded operator behave in a way analagous to the built-in operators you
must make the return value const.

Does this have value?

I don't know.
Jul 22 '05 #3

P: n/a
"DaKoadMunky" <da*********@aol.com> wrote in message
Phlip
friend const Complex operator + (const Complex& lhs, const Complex&
rhs);
That const adds no value

Well, if you believe what some people say about operator overloading and "do as the ints do" then it does make it behave more like a function that returns a built-in type by value.

int a,b,c;
(a+b) = c; //This should not compile

Complex a,b,c;
(a+b) = c; //This will compile if return value is not const.

For reasons I have never grasped a user-defined type returned by value is not an lvalue yet it can be the target of modifying operations. To make the
overloaded operator behave in a way analagous to the built-in operators you must make the return value const.

Does this have value?

I don't know.


Yes, it prevents accidentally modifying a newly returned object, so might
catch code which compiles but may do the wrong thing.

a++ = b;
Jul 22 '05 #4

P: n/a

"Martin Vorbrodt" <mv*******@poczta.onet.pl> skrev i en meddelelse
news:cg**********@news.onet.pl...
which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);

OR either one but a member operator instead of a friend.

Thanx,
Martin

You should prefer

class Complex
{
Complex& operator+=(Complex const& rhs);
......

then you can have:
Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
rhs; }
Jul 22 '05 #5

P: n/a
So how about +=, etc operators.
I assume they return a reference, not a constant referance,
since this code is legal and works just fine:

int a = 1, b = 2, c = 3;
a += b += c;
(a += b) += c;
a += (b += c);
"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m06.aol.com...
That const adds no value
Well, if you believe what some people say about operator overloading and

"do as the ints do" then it does make it behave more like a function that returns a built-in type by value.

int a,b,c;
(a+b) = c; //This should not compile

Complex a,b,c;
(a+b) = c; //This will compile if return value is not const.

For reasons I have never grasped a user-defined type returned by value is not an lvalue yet it can be the target of modifying operations. To make the
overloaded operator behave in a way analagous to the built-in operators you must make the return value const.

Does this have value?

I don't know.

Jul 22 '05 #6

P: n/a
Why would i want to do that? That would modify LHS variable. I don't want
that.
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:pg********************@news000.worldonline.dk ...

"Martin Vorbrodt" <mv*******@poczta.onet.pl> skrev i en meddelelse
news:cg**********@news.onet.pl...
which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);
OR either one but a member operator instead of a friend.

Thanx,
Martin

You should prefer

class Complex
{
Complex& operator+=(Complex const& rhs);
.....

then you can have:
Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
rhs; }

Jul 22 '05 #7

P: n/a
"Martin Vorbrodt" <mv*******@poczta.onet.pl> wrote in message news:cgsrv4
"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
You should prefer

class Complex
{
Complex& operator+=(Complex const& rhs);
.....

then you can have:
Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
rhs; }


That's assuming we want both operators, which is a reasonable assumption.
Why would i want to do that? That would modify LHS variable. I don't want
that.


It's fine. Look closely at the function arguments. The function receives
lhs by value, so it's a copy. However, it should be declared not const.
This should suffice.

Complex operator +(Complex lhs,Complex const& rhs) { return lhs += rhs; }

You can optionally return a const Complex, as indicated in the other
sub-thread to prevent accidental assignment to the returned unnamed
temporary.

const Complex operator +(Complex lhs,Complex const& rhs) { return lhs +=
rhs; }
Jul 22 '05 #8

P: n/a
> "DaKoadMunky" <da*********@aol.com> wrote in message
"Martin Vorbrodt" <mv*******@poczta.onet.pl> wrote in message news:cgsrtm$nd9

We prefer if you reply to posts in place, that is include the quoted text
then your comments to that quoted text, then more quoted text. This makes
it easier for people to read. At work of course, I usually just hit the
reply button as it's faster.
int a,b,c;
(a+b) = c; //This should not compile
So how about +=, etc operators.
I assume they return a reference, not a constant referance,
since this code is legal and works just fine:

int a = 1, b = 2, c = 3;
a += b += c;
(a += b) += c;
a += (b += c);


In return a const object, we're talking about returned a value. Of course,
returning a reference to an object is another story. We have to return a
non-const or const reference as appropriate to the design. As for returning
an object by value, as with operator++ operator+, it doesn't really matter
whether we return the object as const or not, but returning const is a
little safer and could catch strange bugs.
Jul 22 '05 #9

P: n/a

"Martin Vorbrodt" <mv*******@poczta.onet.pl> skrev i en meddelelse
news:cg**********@news.onet.pl...
Why would i want to do that? That would modify LHS variable. I don't want
that.
Because you almost certainly wants the operator+=. For users of your class
it will be confusing if they can write a = a + b but not a += b.

As you can see from my code, this makes operator+ very easy to write - and
it does not have to be a friend function.

Also i must add that i find your reply quite confusing. Do not toppost.

Kind regards
Peter


"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message
news:pg********************@news000.worldonline.dk ...

"Martin Vorbrodt" <mv*******@poczta.onet.pl> skrev i en meddelelse
news:cg**********@news.onet.pl...
which is preferable and WHY?

class Complex {
...
friend Complex operator + (const Complex& lhs, const Complex& rhs);

OR

friend const Complex operator + (const Complex& lhs, const Complex& rhs);
OR either one but a member operator instead of a friend.

Thanx,
Martin

You should prefer

class Complex
{
Complex& operator+=(Complex const& rhs);
.....

then you can have:
Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
rhs; }


Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.