异常和多态问题 [英] exception and polymorphism problem
问题描述
您好我有这个代码:
class异常:公共例外
{
public:
Exception(string m =" exception!"):msg(m){}
~Exception()throw(){}
const char * what(){return msg.c_str(); }
私人:
string msg;
};
int main ()
{
试试
{
抛出异常();
}
catch(例外e)
{
cout<< e.what()<<结束;
}
返回0;
}
当我跑它我没有打印出我的消息异常!但是
St9exception
为什么?
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
why?
推荐答案
On 16 Maj,13:01,josh< xdevel1 ... @ gmail.comwrote:
On 16 Maj, 13:01, josh <xdevel1...@gmail.comwrote:
您好我有这个代码:
class异常:公共例外
{
public:
Exception(string m =" exception!"):msg( m){}
~Exception()throw(){}
const char * what(){return msg.c_str(); }
私人:
string msg;
};
int main()
{
试试
{
抛出异常();
}
catch(例外e)
{
cout<< e.what()<<结束;
}
返回0;
}
当我运行它时,我没有打印出我的消息异常!但是
St9exception
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
因为你按值捕获异常,所以你复制 - 构造一个新的
std ::抛出异常的异常,通过引用捕获异常
它将起作用:
catch(exception& e)>
-
Erik Wikstr?m
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?m
On 5 ??16è?,? ???7ê±01·?, josh< xdevel1 ... @ gmail.comwrote:
On 5??16è?, ????7ê±01·?, josh <xdevel1...@gmail.comwrote:
您好我有这个代码:
>
class异常:公共例外
{
public:
Exception(string m =" exception!"): msg(m){}
~Exception()throw(){}
const char * what(){return msg.c_str(); }
私人:
string msg;
};
int main()
{
试试
{
抛出异常();
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();
总是在抛出点创建一个异常对象。我们引用
它为temp这里。
An exception object is always created at the throw point.We reference
it as "temp" here.
}
catch(例外e)
}
catch(exception e)
异常声明catch子句的行为与
参数声明非常相似。这里,e是由temp初始化的异常类型的另一个对象
(非异常)。类型异常。
The exception declaration of a catch clause behaves very much like a
parameter declaration. Here, e is another object of type exception
(Not Exception) that initialized by the "temp" of type Exception.
{
cout<< e.what()<< ENDL;
{
cout << e.what() << endl;
所以,这里,e.what()引用类的what()成员函数
异常,但不是类异常。
So, here, the e.what() refers to the what() member function of class
Exception, but not class exception.
}
返回0;
}
当我运行它时我没有打印出我的消息异常!但是
St9exception
为什么?
}
return 0;
}
when I run it I don''t have print out my message "exception!" but
St9exception
why?
On 16 Mag,13:04,Erik Wikstr?m< eri ... @ student.chalmers.sewrote:
On 16 Mag, 13:04, Erik Wikstr?m <eri...@student.chalmers.sewrote:
16 Maj,13:01,josh< xdevel1 ... @ gmail.comwrote:
On 16 Maj, 13:01, josh <xdevel1...@gmail.comwrote:
嗨我有这个代码:
Hi I have this code:
class异常:公共例外
{
public:
Exception(string m =" exception!"):msg(m){}
~Exception()throw(){}
const char * what(){return msg.c_str(); }
class Exception : public exception
{
public:
Exception(string m="exception!") : msg(m) {}
~Exception() throw() {}
const char* what() { return msg.c_str(); }
private:
string msg;
private:
string msg;
};
};
int main()
{
试试
{
抛出异常();
}
catch(例外e)
{
cout<< e.what()<< endl;
}
int main()
{
try
{
throw Exception();
}
catch(exception e)
{
cout << e.what() << endl;
}
返回0;
return 0;
}
}
当我运行它时我不是打印出我的消息异常!但是
St9exception
when I run it I don''t have print out my message "exception!" but
St9exception
因为你按值捕获异常,所以你复制 - 构造一个新的
std ::抛出异常的异常,通过引用捕获异常
它将起作用:
catch(exception& e)>
-
Erik Wikstr?m
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?m
是的我忘了...... ..所以它打印9Exception但不是
我的msg字符串可能是我没有覆盖正确的什么()?
PS
我尝试读取exception.h和exception.cpp的源代码,但在我的
gcc源代码中我找不到它!
它们在哪里?
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?
这篇关于异常和多态问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!