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 

Why return *this

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





PostPosted: Wed Jul 27, 2005 5:59 pm    Post subject: Why return *this Reply with quote



In one of the books, the author writes:
Class Complex
{
....
Complex& operator+=(const Complex& Other)
{
real_+=Other.real_;
im_+=Other.im_;
return *this;
}
....
}

Why cant it just be
void operator+=(const Complex& Other)
{
real_+=Other.real_;
im_+=Other.im_;

}

Back to top
Andre Kostur
Guest





PostPosted: Wed Jul 27, 2005 6:04 pm    Post subject: Re: Why return *this Reply with quote



[email]bluekite2000 (AT) gmail (DOT) com[/email] wrote in news:1122487180.619994.180790
@g14g2000cwa.googlegroups.com:

Quote:
In one of the books, the author writes:
Class Complex
{
...
Complex& operator+=(const Complex& Other)
{
real_+=Other.real_;
im_+=Other.im_;
return *this;
}
...
}

Why cant it just be
void operator+=(const Complex& Other)
{
real_+=Other.real_;
im_+=Other.im_;

}



So that one can do:

Complex x;

x += othercomplex += thirdcomplex;

Back to top
Howard
Guest





PostPosted: Wed Jul 27, 2005 6:09 pm    Post subject: Re: Why return *this Reply with quote




<bluekite2000 (AT) gmail (DOT) com> wrote

Quote:
In one of the books, the author writes:
Class Complex
{
...
Complex& operator+=(const Complex& Other)
{
real_+=Other.real_;
im_+=Other.im_;
return *this;
}
...
}

Why cant it just be
void operator+=(const Complex& Other)
{
real_+=Other.real_;
im_+=Other.im_;

}


It could be... but then the result couldn't be used in an expression. Why
limit yourself?

-Howard




Back to top
Rolf Magnus
Guest





PostPosted: Wed Jul 27, 2005 6:34 pm    Post subject: Re: Why return *this Reply with quote

[email]bluekite2000 (AT) gmail (DOT) com[/email] wrote:

Quote:
In one of the books, the author writes:
Class Complex
{
...
Complex& operator+=(const Complex& Other)
{
real_+=Other.real_;
im_+=Other.im_;
return *this;
}
...
}

Why cant it just be
void operator+=(const Complex& Other)
{
real_+=Other.real_;
im_+=Other.im_;

}

It can, but shouldn't. A good rule for operator overloading is: "do it as
int does". And you can e.g. write:

a = b = c;

Therefore, it's good to return a reference to an object, so that you can use
the assignment in other expressions.


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.