鉴于这一样本代码:
#include <iostream>
#include <stdexcept>
class my_exception_t : std::exception
{
public:
explicit my_exception_t()
{ }
virtual const char* what() const throw()
{ return "Hello, world!"; }
};
int main()
{
try
{ throw my_exception_t(); }
catch (const std::exception& error)
{ std::cerr << "Exception: " << error.what() << std::endl; }
catch (...)
{ std::cerr << "Exception: unknown" << std::endl; }
return 0;
}
我获得以下产出:
Exception: unknown
Yet simply making the inheritance of my_exception_t
from std::exception
public
, 我获得以下产出:
Exception: Hello, world!
请允许我向我解释为什么在这种情况下继承事项类型? 在标准中注明参考点。