const std::String& arguments
Is this code correct and stable?
I am thinking about the const char* to const std::string& casting. I'm guessing a temporary string-object will be created, but what is it's lifetime?
Oh, well. I just want to know if i can do this safetly or not.
Code:
void log(const std::string& text)
{
std::string wholeMessage = "Some text here";
wholeMessage += text;
std::cout << wholeMessage << std::endl;
}
int main()
{
log("I'm i safe?");
return 0;
}
/Niklas Andersson
Sweden
Re: const std::String& arguments
It's perfectly safe. The temporary std::string will remain alive as long as the reference to it is alive. (To the end of the function call)
Re: const std::String& arguments
Well, it's only reason to create this temporary object - to be accessible at least within the function. Do you think it's possible that it WOULD be created, but somehow nobody knows for how long (as function is minimum scope it's supposed to exist in)? :D
Re: const std::String& arguments
Great, that was what i thought, but i wasn't sure.
Thanks for your replies :thumb:
/Niklas