Proper method of returning const char* from a function, e.g., overriding
std::exception::what()
When extending std::exception, I am wondering the proper way of overriding
what()?
Lets say I have an exception class :
class MyException : public std::exception {
public:
MyException(const string& _type) : m_type(_type) {}
virtual const char* what() const throw() {
string s = "Error::" + _type;
return s.c_str();
}
}
I have used a static analysis tool on the above code, and it is
complaining that the string s will leave the scope and destroy the memory
associated with the string, so it could potentially be a problem if I use
what() in some part of my code.
If there a proper way of returning a const char* from a function without
these issues retaining proper memory management?
No comments:
Post a Comment