+ 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.
1 Respuesta
+ 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)