|
-
July 10th, 2005, 09:07 AM
#1
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
-
July 10th, 2005, 09:36 AM
#2
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)
Insert entertaining phrase here
-
July 10th, 2005, 10:27 AM
#3
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)?
"Programs must be written for people to read, and only incidentally for machines to execute."
-
July 10th, 2005, 01:55 PM
#4
Re: const std::String& arguments
Great, that was what i thought, but i wasn't sure.
Thanks for your replies
/Niklas
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|