What are temporary instances? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What are temporary instances?

I was reading about explicit call to a constructor in c++. But I see that we can create temporary instances with explicit constructor. But I don't know what is temporary instances and how they works. So please let me know. And can we use them in SoloLearn. Are they exist in any other languages.

14th Jun 2018, 4:14 AM
Samar Singhai
Samar Singhai - avatar
1 Answer
+ 3
A temporary instance/variable/object its created when you create an variable avaible in one statement only because you have not referenced it somewhere... Example: string world= "world" string res= string("hello") + world; Here the unamed string with value "hello" its an temporaty var/instance/object because after the assignement statement it will not exist anymore... Its useful only in that statement... Warn that this is true only with static allocation because with dynamic allocated variables compiler assume that client code allocate/deallocate they (with new/delete keywords and c malloc c family functions)
14th Jun 2018, 6:22 AM
KrOW
KrOW - avatar