H
Henryk
Hello,
what is the scope of an object that is created within one expresseion
of the "?" operator.
Let me try to give an example
#inlcude <string>
std::string somefunc() {
return "Some Text";
}
int main(int argc, char *argv[]) {
// does p1 contain a valid address?
const char *p1 = true ? ::somefunc().c_str() : NULL;
// working alternative?
std::string s = true ? ::somefunc() : "";
const char p2 = s.c_str();
return 0;
}
My concern: somefunc() returns a temporary string object. c_str()
returns a valid const char pointer to the content of that temporary
object. But does the temporary string object still exist outside the
"?" operator line? If not, then pointer p1 points to freed memory.
Thanks for your help
what is the scope of an object that is created within one expresseion
of the "?" operator.
Let me try to give an example
#inlcude <string>
std::string somefunc() {
return "Some Text";
}
int main(int argc, char *argv[]) {
// does p1 contain a valid address?
const char *p1 = true ? ::somefunc().c_str() : NULL;
// working alternative?
std::string s = true ? ::somefunc() : "";
const char p2 = s.c_str();
return 0;
}
My concern: somefunc() returns a temporary string object. c_str()
returns a valid const char pointer to the content of that temporary
object. But does the temporary string object still exist outside the
"?" operator line? If not, then pointer p1 points to freed memory.
Thanks for your help