return value of = operator
-
Hi, what is the reason for the Cxyz& return value with *this parameter of = operators? Dr-Kuulun
Not sure exactly what you are asking, but
this
is a pointer while*this
is the actual object being pointed to. Since theoperator=
method is returning a reference to the actual object, that's why*this
would need to be returned.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Hi, what is the reason for the Cxyz& return value with *this parameter of = operators? Dr-Kuulun
if u define return type as Cxyz&, u should return *this, otherwise what you can return? u can define return type other way, such as void or BOOL.
A special image tool for Windows C++ programmers, don't miss it! The world unique Software Label Maker is waiting for you and me ... A nice hyper tool for optimizing your Microsoft html-help contents.
-
Hi, what is the reason for the Cxyz& return value with *this parameter of = operators? Dr-Kuulun
The reason is simpler than you may think. Its is so that you can make multiple assignment work efficiently. You expect this to work:
int a, b, c;
a = b = c = 1;
So you also accept this to work:
CFwibble a, b, c;
c.DoSomethingtoInitialisethestruct;
a = b = c;But the last line is equivalent to:
a = (b = c);
which means
b = c
has to be something you can assign to another struct / class / object of the same kind. You could make the return valuexyz
, but that would mean creating a temporary copy on the stack, which is inefficient. Thexyz &
means a reference tob
is passed, so no temporary copy is made. For proper purity, you should also make the reference const, to prevent b from being messed about with...class xyz
{
...
const xyz &operator=(const xyz &rhs)
{
...
return *this;
}
};I hope that made sense for you! Iain. -- modified at 19:54 Monday 3rd April, 2006
-
The reason is simpler than you may think. Its is so that you can make multiple assignment work efficiently. You expect this to work:
int a, b, c;
a = b = c = 1;
So you also accept this to work:
CFwibble a, b, c;
c.DoSomethingtoInitialisethestruct;
a = b = c;But the last line is equivalent to:
a = (b = c);
which means
b = c
has to be something you can assign to another struct / class / object of the same kind. You could make the return valuexyz
, but that would mean creating a temporary copy on the stack, which is inefficient. Thexyz &
means a reference tob
is passed, so no temporary copy is made. For proper purity, you should also make the reference const, to prevent b from being messed about with...class xyz
{
...
const xyz &operator=(const xyz &rhs)
{
...
return *this;
}
};I hope that made sense for you! Iain. -- modified at 19:54 Monday 3rd April, 2006
Iain Clarke wrote:
CFwibble a, b, c;
5 just for the class name! :-D
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
The reason is simpler than you may think. Its is so that you can make multiple assignment work efficiently. You expect this to work:
int a, b, c;
a = b = c = 1;
So you also accept this to work:
CFwibble a, b, c;
c.DoSomethingtoInitialisethestruct;
a = b = c;But the last line is equivalent to:
a = (b = c);
which means
b = c
has to be something you can assign to another struct / class / object of the same kind. You could make the return valuexyz
, but that would mean creating a temporary copy on the stack, which is inefficient. Thexyz &
means a reference tob
is passed, so no temporary copy is made. For proper purity, you should also make the reference const, to prevent b from being messed about with...class xyz
{
...
const xyz &operator=(const xyz &rhs)
{
...
return *this;
}
};I hope that made sense for you! Iain. -- modified at 19:54 Monday 3rd April, 2006