There are some cases:
case 1:
string("test");
int i = 1;
This is a temporary object. It will be destructed as soon as we arrive int i = 1;
. Am I right?
case 2:
const char * p = string("test").c_str();
int i = 1;
cout<< p;
I think p will point an illegal memory when we arrive int i = 1;
. But I can always cout
the right string. I'm just lucky or p is NOT illegal?
case 3:
void fun()
{
throw Exception("error");
}
int main()
{
try
{
fun();
}catch(const Exception & e)
{
cout << e.description();
}
}
Can we throw
a temporary object in a function and catch it at its higher-level function with a const reference?