Jump to content
  • Advertisement
Sign in to follow this  
baker

quick question, "string myfuntion(string &param)" vs "string myfuntion(string param)"

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

the '&' in '¶m' means that whatever I do to param will change what is in 'param's memory location right?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Yes. If you'd like to prevent that, pass by const reference.

Share this post


Link to post
Share on other sites
Yes. It also means that you do not create another string in memory. So you only have a to create a reference and you manipulate the object/value that the reference points to.

Share this post


Link to post
Share on other sites
void myfunc(string param) is called Pass-By-Value, it will create a copy of param.

void myfunc(string& param) is called Pass-By-Reference, It will pass a reference, and thus it should be marginally faster. However param can be altered by myfunc.

void myfunc(const string& param) is called Pass-By-Const-Reference, It will pass a reference, and will prevent myfunc() from making any changes to param.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!