Re: exception and polymorphism problem
On 16 Mag, 13:04, Erik Wikstr=F6m <eri...@student.chalmers.se> wrote:
On 16 Maj, 13:01, josh <xdevel1...@gmail.com> wrote:
Hi I have this code:
class Exception : public exception
{
public:
Exception(string m="exception!") : msg(m) {}
~Exception() throw() {}
const char* what() { return msg.c_str(); }
private:
string msg;
};
int main()
{
try
{
throw Exception();
}
catch(exception e)
{
cout << e.what() << endl;
}
return 0;
}
when I run it I don't have print out my message "exception!" but
St9exception
Because you catch the exception by value, so you copy-construct a new
std::exception from the Exception that was thrown, catch the exception
by reference instead and it will work:
catch (exception& e)
--
Erik Wikstr=F6m
yes I've forgotten that...... so it print 9Exception but not
my msg string may be I didn't override the right what() ?
P.S.
I try to read the source of exception.h and exception.cpp but in my
gcc source I didn't find it!
where are them?