G
gerald.dalley
I've been trying to pin down the scoping rules for temporary variables
in C++. I know that doing something like:
string s("abc");
const char *t = (s+"def").c_str();
cout << t;
is invalid since (s+"def") creates a temporary which goes out of scope,
thus leaving t a dangling pointer. What I'm wondering is whether
temporaries go out of scope when their expression terminates or when
their statement terminates. For example, is the following wrong?
string s("abc");
cout << (s+"def").c_str();
in C++. I know that doing something like:
string s("abc");
const char *t = (s+"def").c_str();
cout << t;
is invalid since (s+"def") creates a temporary which goes out of scope,
thus leaving t a dangling pointer. What I'm wondering is whether
temporaries go out of scope when their expression terminates or when
their statement terminates. For example, is the following wrong?
string s("abc");
cout << (s+"def").c_str();