fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class A
  6. {
  7. public:
  8. A()
  9. {
  10. cout<<"A init"<<endl;
  11. showptr();
  12. cout<<"throwing..."<<endl;
  13. throw 1;
  14. }
  15. ~A()
  16. {
  17. cout<<"A deinit"<<endl;
  18. showptr();
  19. }
  20.  
  21. void showptr()
  22. {
  23. cout<<"A "<<this<<endl;
  24. }
  25. };
  26.  
  27. class B
  28. {
  29. public:
  30. B() : ClassChain(NULL)
  31. {
  32. ClassChain = new A;
  33. cout<<"B init"<<endl;
  34. showptr();
  35. }
  36. ~B()
  37. {
  38. cout<<"B deinit"<<endl;
  39. showptr();
  40. delete ClassChain;
  41. ClassChain = NULL;
  42. }
  43.  
  44. A* ClassChain;
  45.  
  46. void showptr()
  47. {
  48. cout<<"B "<<this<<endl;
  49. }
  50. };
  51.  
  52. int main()
  53. {
  54. B* myClass = NULL;
  55. try
  56. {
  57. myClass = new B;
  58. }
  59. //guaranteed throw
  60. catch(int)
  61. {
  62. delete myClass;
  63. myClass = NULL;
  64. }
  65. return 0;
  66. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
A init
A 0x9738018
throwing...