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