C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to write an operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Martin Vorbrodt
Guest





PostPosted: Sat Aug 28, 2004 8:47 pm    Post subject: how to write an operator Reply with quote



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


Back to top
Phlip
Guest





PostPosted: Sat Aug 28, 2004 10:09 pm    Post subject: Re: how to write an operator Reply with quote



Martin Vorbrodt wrote:

Quote:
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.

Quote:
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/bin/view/Main/TestFirstUserInterfaces



Back to top
DaKoadMunky
Guest





PostPosted: Sun Aug 29, 2004 1:30 am    Post subject: Re: how to write an operator Reply with quote



Quote:
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.



Back to top
Siemel Naran
Guest





PostPosted: Sun Aug 29, 2004 4:29 am    Post subject: Re: how to write an operator Reply with quote

"DaKoadMunky" <dakoadmunky (AT) aol (DOT) com> wrote in message
Quote:
Phlip

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


Quote:
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;



Back to top
Peter Koch Larsen
Guest





PostPosted: Sun Aug 29, 2004 1:56 pm    Post subject: Re: how to write an operator Reply with quote


"Martin Vorbrodt" <mvorbrodt (AT) poczta (DOT) onet.pl> skrev i en meddelelse
news:cgqr01$567$1 (AT) news (DOT) onet.pl...
Quote:
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; }



Back to top
Martin Vorbrodt
Guest





PostPosted: Sun Aug 29, 2004 3:15 pm    Post subject: Re: how to write an operator Reply with quote

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" <dakoadmunky (AT) aol (DOT) com> wrote

Quote:
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.





Back to top
Martin Vorbrodt
Guest





PostPosted: Sun Aug 29, 2004 3:16 pm    Post subject: Re: how to write an operator Reply with quote

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


"Peter Koch Larsen" <pklspam (AT) mailme (DOT) dk> wrote

Quote:

"Martin Vorbrodt" <mvorbrodt (AT) poczta (DOT) onet.pl> skrev i en meddelelse
news:cgqr01$567$1 (AT) news (DOT) 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; }





Back to top
Siemel Naran
Guest





PostPosted: Sun Aug 29, 2004 4:06 pm    Post subject: Re: how to write an operator Reply with quote

"Martin Vorbrodt" <mvorbrodt (AT) poczta (DOT) onet.pl> wrote

Quote:
"Peter Koch Larsen" <pklspam (AT) mailme (DOT) 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.

Quote:
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; }



Back to top
Siemel Naran
Guest





PostPosted: Sun Aug 29, 2004 4:09 pm    Post subject: Re: how to write an operator Reply with quote

Quote:
"DaKoadMunky" <dakoadmunky (AT) aol (DOT) com> wrote in message
"Martin Vorbrodt" <mvorbrodt (AT) poczta (DOT) onet.pl> wrote


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.

Quote:
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.



Back to top
Peter Koch Larsen
Guest





PostPosted: Tue Aug 31, 2004 7:05 am    Post subject: Re: how to write an operator Reply with quote


"Martin Vorbrodt" <mvorbrodt (AT) poczta (DOT) onet.pl> skrev i en meddelelse
news:cgsrv4$qam$1 (AT) news (DOT) onet.pl...
Quote:
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

Quote:


"Peter Koch Larsen" <pklspam (AT) mailme (DOT) dk> wrote in message
news:pglYc.41619$Vf.2198766 (AT) news000 (DOT) worldonline.dk...

"Martin Vorbrodt" <mvorbrodt (AT) poczta (DOT) onet.pl> skrev i en meddelelse
news:cgqr01$567$1 (AT) news (DOT) 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; }







Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.