RE: Fw: [Effective-cpp] Item 10: Have assignment operators return areference to *this

Frances Buontempo buontempo at lineone.net
Fri Jun 9 07:57:31 EDT 2006


>> Item 10: Have assignment operators return a reference to *this 
>> Other items referenced: 54.
>>
>> Scott points out that by convention assignments can be chained together.
>The example given being
>>   x = y = z = 15,
>> which results in x, y and z each being set to 15.
>>
>> Scott also notes that assignments are right associative (that is assignments
>> are made from right to left) so that for the given example 15 is assigned
to
>> z, the value of z is assigned to y and the value of y is assigned to x.
>>
>> Scott advises maintaining this behaviour in your own classes by having
>> assignment operators return a reference to *this. He gives code examples
>for
>> over-loading = and +=.
>>
>> He points out that this is only a convention, but one that is followed
by
>> all built-in and standard library types; hence your classes should do
the
>> same unless you have a good reason not to.
>>
>> Things to Remember
>> Have assignment operators return a reference to *this.
>>

Guess Carl is going to get away with a very short discussion summary!

I assume this means "return a reference to *this" as opposed to "return a
CONST ref to *this"?

Frances.


___________________________________________________________

Tiscali Broadband from 14.99 with free setup!
http://www.tiscali.co.uk/products/broadband/






More information about the Effective-cpp mailing list