how to write an operator

M

Martin Vorbrodt

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
 
P

Phlip

Martin said:
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);
 
D

DaKoadMunky

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.
 
S

Siemel Naran

DaKoadMunky said:
Phlip rhs);
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;
 
P

Peter Koch Larsen

Martin Vorbrodt said:
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; }
 
M

Martin Vorbrodt

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);
 
M

Martin Vorbrodt

Why would i want to do that? That would modify LHS variable. I don't want
that.
 
S

Siemel Naran

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; }
 
S

Siemel Naran

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.
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.
 
P

Peter Koch Larsen

Martin Vorbrodt said:
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
 

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
474,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top