A
Andrew Tomazos
Please consider the below class MyError. My question is is it safe/
correct to store a std::string in a std::exception subclass and then
return string::c_str() on exception::what(). If not what is the
correct way to do this then? Thanks, Andrew.
#include <exception>
#include <string>
#include <sstream>
using namespace std;
class MyError : public exception
{
public:
MyError(A a, B b, C c)
{
ostringstream os;
os << "MyError occured because A =" << a.str() << ", B = " <<
b.str() << " and C = " << c.str() << endl;
m_sWhat = os.str();
}
virtual const char* what() const throw() { return
m_sWhat.c_str(); }
virtual ~MyError() throw() {}
private:
string m_sWhat;
};
correct to store a std::string in a std::exception subclass and then
return string::c_str() on exception::what(). If not what is the
correct way to do this then? Thanks, Andrew.
#include <exception>
#include <string>
#include <sstream>
using namespace std;
class MyError : public exception
{
public:
MyError(A a, B b, C c)
{
ostringstream os;
os << "MyError occured because A =" << a.str() << ", B = " <<
b.str() << " and C = " << c.str() << endl;
m_sWhat = os.str();
}
virtual const char* what() const throw() { return
m_sWhat.c_str(); }
virtual ~MyError() throw() {}
private:
string m_sWhat;
};