how long can a temporary object live?

J

Jess

Hello,

If a function's signature is:

T f();

and it returns its result (of type T) by value, then the result will
be copied into a temporary place, as a temporary object. If it is a
temporary object, then can I still call T's function "g" like the
following?

(f()).g()

I'm wondering how long a temporary object can live, so that I can
modify its states via "g", or do some even more complicated operations
on this temporary object.

Thanks,
Jess
 
J

jg

Hello,

If a function's signature is:

T f();

and it returns its result (of type T) by value, then the result will
be copied into a temporary place, as a temporary object. If it is a
temporary object, then can I still call T's function "g" like the
following?

(f()).g()

Definitely. The temporary for the return value is valid in the
calling context.
However, if you don't assign f() to a variable, it will be
inaccessible, thus
no use at all.
I'm wondering how long a temporary object can live, so that I can
modify its states via "g", or do some even more complicated operations
on this temporary object.

T t = f();
Use t to perform your operations as long as t is live.

JG
 
J

Jim Langston

Jess said:
Hello,

If a function's signature is:

T f();

and it returns its result (of type T) by value, then the result will
be copied into a temporary place, as a temporary object. If it is a
temporary object, then can I still call T's function "g" like the
following?

(f()).g()

I'm wondering how long a temporary object can live, so that I can
modify its states via "g", or do some even more complicated operations
on this temporary object.

Yes, and it's most useful I've found for passing built up character arrays
to a C function expectign a c style string.

somefunctionexpectingcstr( (std::string("The data text is :") + datapointer
+ ":").c_str() );
or some such when I find it more meaningful to build the string in the
function parm for whatever reason.

I acutally use very similar to some graphic display of text that expects a C
string.
 
J

Jess

Thanks! Jess

Definitely. The temporary for the return value is valid in the
calling context.
However, if you don't assign f() to a variable, it will be
inaccessible, thus
no use at all.




T t = f();
Use t to perform your operations as long as t is live.

JG
 
J

James Kanze

If a function's signature is:
and it returns its result (of type T) by value, then the result will
be copied into a temporary place, as a temporary object. If it is a
temporary object, then can I still call T's function "g" like the
following?

I'm wondering how long a temporary object can live, so that I can
modify its states via "g", or do some even more complicated operations
on this temporary object.

There are a few special cases where the lifetime is extended,
but normally, the lifetime of a temporary is until the end of
the full expression in which it was constructed. So things like
f().g() are no problem (supposing, of course, that T has a
member function g()).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top