473,396 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how long can a temporary object live?

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

May 4 '07 #1
4 1579
jg
On May 3, 8:02 pm, Jess <w...@hotmail.comwrote:
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

May 4 '07 #2
"Jess" <wd***@hotmail.comwrote in message
news:11*********************@h2g2000hsg.googlegrou ps.com...
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.
May 4 '07 #3
Thanks! Jess

On May 4, 1:23 pm, jg <jgu...@gmail.comwrote:
On May 3, 8:02 pm, Jess <w...@hotmail.comwrote:
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

May 4 '07 #4
On May 4, 5:02 am, Jess <w...@hotmail.comwrote:
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.
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()).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
19
by: Kai-Uwe Bux | last post by:
Hi folks, I have trouble writing a class, derving from stringstream, that collects item and once it's done will write them to std::cout in one go. It works fine except when I use it as a...
2
by: John Alesse | last post by:
Hi, I've created a very simple winforms control using the c# wizard in VS .net 2003 that is nothing but a System.Windows.Forms.UserControl. There are no other controls on the form It takes IE 12...
35
by: aNt17017 | last post by:
This is my code: long fact(int n) { if (n == 0) return(1); if(n > 100) { printf("\t\tERROR: %d is too large for factorial.\n", n); return 1;
8
by: CodeCracker | last post by:
Is it possible to write such an operator function? watch that there is a space in between unsigned and long. Does this mean I have to write two operator function one for long and another for...
2
by: rob | last post by:
Hi, I'm a bit confused by the mapping of the native types int and long to UInt32. I'm trying to bind a tracking reference to a native value type and it works for Int32 % -int but not for Int32 %...
10
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess...
8
by: Phil Endecott | last post by:
Dear Experts, I'm surprised to find that std::max doesn't work (i.e. won't compile) if the arguments are not of exactly the same type, e.g. one is a short and the other is a long: #include...
17
by: Klaas Vantournhout | last post by:
Hi all, I was wondering if it is possible if you can check in a function if one of the arguments is temporary. What I mean is the following. A is a class, foo is a function returning a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.