473,659 Members | 3,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1586
jg
On May 3, 8:02 pm, Jess <w...@hotmail.c omwrote:
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******** *************@h 2g2000hsg.googl egroups.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.

somefunctionexp ectingcstr( (std::string("T he 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.c omwrote:
On May 3, 8:02 pm, Jess <w...@hotmail.c omwrote:
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.c omwrote:
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 objektorientier ter Datenverarbeitu ng
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
2270
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are still in CVS) got the basic abilities of a system shell (like bash). Actually, using it as a shell is rather comfortable. This all made me think... Why do we write simple scripts to do simple things? Why do we serialize data to flat text files in...
19
2475
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 temporary. Here is a tiny test programm: #include <iostream> #include <sstream>
2
2066
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 seconds to load this control into the page. CPU usage is at only 1% to 2% during the 12 sec All of this is on a single machine XP Pro sp1 (all critical updates loaded) 2.6GHz, P4, 512MB ra IE v6.0.2800.1106.xpsp2.030422-1633 IIS 5.1 ..net...
35
3960
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
3245
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 unsigned? Regards,
2
4122
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 % -long. This page http://msdn2.microsoft.com/en-gb/library/0wf2yk2k(VS.80).aspx states that both int & long map to Int32. The following code prints "Hello World 1" rather than the expected
10
2567
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 is that it's not destroyed, but I'm not sure. I have the following program: #include<iostream>
8
13700
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 <algorithm> int f(short s, long l) { return std::max(s,l);
17
2084
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 and bar is a function using A is an argument returning something else class A;
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8531
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8628
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7359
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2754
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.