Jump to content
  • Advertisement
Sign in to follow this  
Chuncho

[C++] Overloading operator+

This topic is 2839 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

So, is it correct to pass a value by const-reference in this situation? For what I read, it isn't, so I won't do it. But is it always incorrect, or there are some situations where is correct?

What if I pass the value just as const?

Like : ...::operator+(const int)...

Would that be correct? Because I'm not supossed to change the value of that argument.

Thanks again. :)

Share this post


Link to post
Share on other sites
Advertisement

So, is it correct to pass a value by const-reference in this situation? For what I read, it isn't, so I won't do it. But is it always incorrect, or there are some situations where is correct?

What if I pass the value just as const?

Like : ...::operator+(const int)...

Would that be correct? Because I'm not supossed to change the value of that argument.

Thanks again. :)
You can do that, but using const in conjunction with pass-by-value only serves to make sure that the function doesn't internally modify the copy of that value. E.g. It means that you can look at the function signature and know that the value will be the same at the end of the function as it was at the top.
It enforces things upon itself rather than enforcing things on the caller.

Share this post


Link to post
Share on other sites

[quote name='Chuncho' timestamp='1294943043' post='4758393']
So, is it correct to pass a value by const-reference in this situation? For what I read, it isn't, so I won't do it. But is it always incorrect, or there are some situations where is correct?

What if I pass the value just as const?

Like : ...::operator+(const int)...

Would that be correct? Because I'm not supossed to change the value of that argument.

Thanks again. :)
You can do that, but using const in conjunction with pass-by-value only serves to make sure that the function doesn't internally modify the copy of that value. E.g. It means that you can look at the function signature and know that the value will be the same at the end of the function as it was at the top.
It enforces things upon itself rather than enforcing things on the caller.
[/quote]


Ok, now I'm much clear. Thanks a lot again. :)

Share this post


Link to post
Share on other sites

So, is it correct to pass a value by const-reference in this situation? For what I read, it isn't, so I won't do it. But is it always incorrect, or there are some situations where is correct?

It would be correct, but inefficient, to pass an int by const reference. For primitives passing by const reference adds overhead for no particular value. (Unless for some reason the reference needs to be stored. This, however, opens a whole mess of issues regarding lifetime.)

Share this post


Link to post
Share on other sites
Ok, I got it. Thanks a lot again. I really appreciate your help guys.

PS : Sorry for bumping the post again and again.

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!