Jump to content
  • Advertisement
Sign in to follow this  
C J W

Strings as parameters

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

If I have a function which takes a reference to a string as a parameter, would calling the function with a character string be ok? Are there any problems which could arise? Is this code horridly ugly and should never be written like this? I'm also curious as to why this works? Is there a string constructor which takes a const char* used? Example:
Function(const std::string& string)
{
// Do something magical here with string
}

aFunctionCallingThatFunction()
{
Function("hello"); // This compiles and appears to work
}




Share this post


Link to post
Share on other sites
Advertisement
That code shouldn't compile. While there is a std::string constructor that takes a const char *, it's illegal to bind an temporary to a non-const reference. It may compile under MSVC as MSVC has a non-standard extension that allows non-const references to be bound to rvalues. Change it to a const reference and everything should be fine.

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!