fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. struct ex1
  6. {
  7. std::string reason;
  8. ex1(std::string reason_): reason(reason_) {};
  9. virtual std::string what()
  10. {
  11. return reason;
  12. }
  13. };
  14.  
  15. struct ex2 : ex1
  16. {
  17. std::string comment;
  18. ex2(std::string reason_, std::string comment_) : ex1(reason_), comment(comment_) {}
  19. virtual std::string what()
  20. {
  21. return reason + " " + comment;
  22. }
  23. };
  24.  
  25. int main()
  26. {
  27. try {
  28. throw ex2("Hello", "there");
  29. } catch (ex1 e) {
  30. std::cout << e.what() << '\n';
  31. }
  32. try {
  33. throw ex2("Hello", "there");
  34. } catch (ex1& e) {
  35. std::cout << e.what();
  36. }
  37. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Hello
Hello there