Topic: Lifetime of temporary
Author: jskim@rose.etri.re.kr (Kim Jungsun)
Date: 1995/07/07 Raw View
I forgot who posted this code originally, but while I was reading a thread
regarding this code, I had one thing to confirm about the lifetime of a
temporary object.
CString SayHello (void) { return "Hello"; }
void TextOut (const char* str) { cout << str; }
int main(void)
{
TextOut (SayHello());
return 0;
}
The original poster worried about the fact that the temporary object created
after SayHello() will be destroyed after implicit conversion by "const char*".
And I guess Steve Clammage gave a comment on that. I checked the book "The
Design and evolution ..." section 6.3.2. According to that book, the temporary
should not be destroyed until the end of TextOut (SayHello()).
The book gives a clear example on that using the following example:
void f(String s1, String s2)
{
printf("%s", s1+s2); // ok
...
}
According to the WP, the temporary object is destroyed at the end of full
expression which created the object. And the end of full expression is
TextOut (SayHello()) not SayHello(). Maybe I am wrong.
--Jungsun