Sign in to follow this  

Strings as parameters

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

This topic is 4374 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this