Jump to content
  • Advertisement
Sign in to follow this  
PiCkLeD

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.

If you intended to correct an error in the post then please contact us.

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 this post


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


In a word, yes :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin

In 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by PiCkLeD
There 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 ;)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
:]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!