I have a fairly simple question in C ++, consider a function that takes some input parameters and creates std::string , which is from those parameters that are listed below:
std::string constructString( int some_parameter ) { std::stringstream ss;
I understand that the stringstream object goes out of scope when the function returns, but doesnβt also make this invalid for the constructed string?
What happens if I change the return type to const char * and return ss.str().c_str() instead?
Code like the one above seems to work, but I suspect that it was only because the memory containing the βtemporaryβ object was not overwritten by something else when I use it?
I must admit that I am rather confused in such situations as a whole, I would be grateful if someone could explain all these "temporary objects" - everything for me (or just point me in the right direction).
thanks in advance
Dex3
source share