fork(1) download
  1. #include <iostream>
  2. #include <exception>
  3.  
  4. class Test {
  5. public:
  6. Test() : a(7), b(new char('c'))
  7. {
  8. }
  9.  
  10. Test(Test& other) : a(other.a)
  11. {
  12. try {
  13. if (other.b == nullptr)
  14. throw std::exception();
  15.  
  16. b = new char(*other.b);
  17. }
  18. catch (std::exception &e) {
  19. b = new char('d');
  20. }
  21. }
  22.  
  23. public:
  24. int a;
  25. char* b;
  26. };
  27.  
  28. int main()
  29. {
  30. Test a;
  31. Test b(a);
  32.  
  33. std::cout << b.b << std::endl;
  34. delete a.b;
  35. a.b = nullptr;
  36.  
  37. Test c(a);
  38. std::cout << c.b << std::endl;
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
c
d