Best way to return a const char* from a string
I am trying to overload the what() method of the exception class which returns a const char*.
What's the best way to do it if I have a std::string on the stack, storing the string I want to return?
Obviously
Code:
return mystr.c_str();
is not a good idea because mystr would be deallocated by the time the caller reads the string.
Allocating a buffer of the same size on the stack and copying the string to it will work, but isn't that leaky? ("delete[] e.what();" just looks... wrong to me)
What's the conventional way to go about doing this?