This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
A query on C++ Temporary Objects
- From: Arijit Das <arijit_engg2001 at yahoo dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Tue, 18 Apr 2006 00:27:51 -0700 (PDT)
- Subject: A query on C++ Temporary Objects
Stroustroup, in his book "The C++ Programming
Language" (Section 10.4.10), says that 'Temporary
objects are destroyed when its reference or named
object goes out of scope.'
His statement made full sense for Reference but for
named objects, I didn't understand why should the
destruction of the temporary object wait till the
named object goes out of scope? Why can't it get
destructed immediately after having initialized the
named object? Is this a step towards helping
developers who didn't bother to write a 'deep' copy
constructor and as a result, their named objects are
referring to something also being referred by the
temporary object because of a shallow copy?
Example:
string ss = s1 + s2;
//The temporary object created to initialize ss should
be detroyable now...no?
Here, a temporary object is created which stores the
result of "s1 + s2" and then, that object is used for
initializing ss using its copy constructor. But my
confusion is:
Why should the destruction of the temporary object
wait for the named object (ss in this case) to go out
of scope? Why can't the temporary object get
destructed immediately? I guess there is no more
reference left behind for that temporary object.
Please correct me if I am wrong.
Any help on this is highly appreciated.
Thanks,
Arijit
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com