Public Group

# Operator Question

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was just trying to understand what the difference is here when returning by reference as 'this' is still modified even without the &reference?. Getting confused :/

test& operator = (const test& a)
{
x = a.x;
return *this;
}

//Without test&
test operator = (const test& a)
{
x = a.x;
return *this;
}

//Whats the difference here without test&?
//It does the same thing, so whats the benefit if anyone can show my why please.

//Is it primarily to reduce uneccessary copying of the l-value for optimization and speed?



##### Share on other sites
Quote:
 Is it primarily to reduce uneccessary copying of the l-value for optimization and speed?

In a word, yes :)

##### Share on other sites
Quote:
 Original post by SharlinIn a word, yes :)

With a diffrent word, no.

if you don't return a refrence you can't get chain-calls to work as desired

i.e:
(a = b) += c;//something like this

you'll also break the semantics of what many expect the call

&(a = b); //a bit silly but you get the point

would get the adress of the temporary instead of what most people would assume
the a object now being a copy of b

as a rule of thumb you always want to return by refrence from op= operators.

When in doubt, do as int does.

##### Share on other sites
Thank You Digital,

Now I can see the true benefit of it.

There so many subtle differences in c++ it's much fun /cry

##### Share on other sites
Quote:
 Original post by PiCkLeDThere so many subtle differences in c++ it's much fun /cry

You can say that again, but as you can see the helpful and friendly folks at gamedev will always supply you with at least two! (often mutualy exclusive) conclusive answers! now where else do you find a better deal than that ;)

:]

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5
Rutin
12

• 12
• 12
• 58
• 14
• 15
• ### Forum Statistics

• Total Topics
632119
• Total Posts
3004208

×