#include <iostream>
#include <string>
struct ex1
{
std::string reason;
ex1(std::string reason_): reason(reason_) {};
virtual std::string what()
{
return reason;
}
};
struct ex2 : ex1
{
std::string comment;
ex2(std::string reason_, std::string comment_) : ex1(reason_), comment(comment_) {}
virtual std::string what()
{
return reason + " " + comment;
}
};
int main()
{
try {
throw ex2("Hello", "there");
} catch (ex1 e) {
std::cout << e.what() << '\n';
}
try {
throw ex2("Hello", "there");
} catch (ex1& e) {
std::cout << e.what();
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKCnN0cnVjdCBleDEKewogICAgc3RkOjpzdHJpbmcgcmVhc29uOwogICAgZXgxKHN0ZDo6c3RyaW5nIHJlYXNvbl8pOiByZWFzb24ocmVhc29uXykge307CiAgICB2aXJ0dWFsIHN0ZDo6c3RyaW5nIHdoYXQoKQogICAgewogICAgICAgIHJldHVybiByZWFzb247CiAgICB9Cn07CgpzdHJ1Y3QgZXgyIDogZXgxCnsKICAgIHN0ZDo6c3RyaW5nIGNvbW1lbnQ7CiAgICBleDIoc3RkOjpzdHJpbmcgcmVhc29uXywgc3RkOjpzdHJpbmcgY29tbWVudF8pIDogZXgxKHJlYXNvbl8pLCBjb21tZW50KGNvbW1lbnRfKSB7fQogICAgdmlydHVhbCBzdGQ6OnN0cmluZyB3aGF0KCkKICAgIHsKICAgICAgICByZXR1cm4gcmVhc29uICsgIiAiICsgY29tbWVudDsKICAgIH0KfTsKCmludCBtYWluKCkKewogICAgdHJ5IHsKICAgICAgICB0aHJvdyBleDIoIkhlbGxvIiwgInRoZXJlIik7CiAgICB9IGNhdGNoIChleDEgZSkgewogICAgICAgIHN0ZDo6Y291dCA8PCBlLndoYXQoKSA8PCAnXG4nOwogICAgfQogICAgdHJ5IHsKICAgICAgICB0aHJvdyBleDIoIkhlbGxvIiwgInRoZXJlIik7CiAgICB9IGNhdGNoIChleDEmIGUpIHsKICAgICAgICBzdGQ6OmNvdXQgPDwgZS53aGF0KCk7CiAgICB9Cn0=