Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have some doubt in temporaries in C++.
I did not exactly understand the concept of temporaries. I already searched in google, but I did not get any satisfactory explanation of this and most importantly why we use this? So guys please let me give some good explanation with example for that i can understand easily.

...Waiting for your replies...



Regards.
Johny...
Posted

A simple way to "tune" on them is thinking on how compute an expression like
z = (a+b)*c;


What does multiply c?
"The result of a+b", but it has to be stored somehow as result of + just for the time neede to * to compute.

Now what about
z = operator*(operator+(a,b),c);


This is the same as above: the function named "operator+" return a result,
that is then passed to another function - operator*, in this case - in the form of a temporary value.

Much like
z = f(g(a,b),c);

expressed as
tmp = g(a,b);
z = f(tmp,c);


Of course, this is not completely exhaustive about temporaries, but is just a way to imagine them in a case they have to come to existence.
 
Share this answer
 
Comments
pasztorpisti 2-Sep-12 16:21pm    
5ed, this explanation is better for begginner C++ coders. Maybe the other beast - the function return value copying would worth mentioning, thats easy to understand and might happen often in regular c++ code.
Temporaries are called rvalues and non-temporaries are called lvalues. Named objects in your code are lvalues, and temporary object that are the results of your (sub)expressions are temporary objects - rvalues. For example (x+y) is an rvalue and (x) is an lvalue. Every expression is either an lvalue or rvalue. You can easily distinguis between the two: you can not get a pointer to an rvalue so you can not say &(x+y) while you can do this with &x. The C++11 introduces a very cool feature to handle temporaries more efficiently, its the rvalue reference. To read about temporaries and this new C++11 feature read this lengthy but superb explaining article: http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx[^]
 
Share this answer
 
v3
Comments
Emilio Garavaglia 2-Sep-12 15:54pm    
r-values ARE object and are constructed, copied and destroyed like every other object. (think to x and y as std::string, instead of integers: x+y is a std::string itself, constructed on the return of operator+, and made available to the next operation in the chain.

The fact you cannot take the address is due to its "temporary nature", that makes that address dangling as soon the expression gets evaluated. You cannot get the address because their is anything "persistent" that survives between consecutives sequence points.
pasztorpisti 2-Sep-12 16:13pm    
Good points. Thinking it over you are right. Thanks for pointing this out. I'm correcting the answer according to this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900