fork download
  1. #include <iostream>
  2. using namespace std;
  3. #define MAX 3
  4.  
  5. class hello_world
  6. {
  7. char *const buf;
  8. int stack_ptr, destructor_calls;
  9. bool init;
  10. public:
  11. // The recursive constructor
  12.  
  13. hello_world(char *const &str, int i = 0)
  14. : buf(str), stack_ptr(0), init(false), destructor_calls(0)
  15. {
  16. if (i == MAX)
  17. {
  18. buf[i] = '\0';
  19. cout << buf << endl;
  20. return;
  21. }
  22. buf[i] = '0';
  23. hello_world::hello_world(str, i + 1);
  24. buf[i] = '1';
  25. hello_world::hello_world(str, i + 1);
  26. }
  27.  
  28. // The recusive destructor
  29.  
  30. ~hello_world()
  31. {
  32. ++destructor_calls;
  33. if (!init) { cerr << "A destructor call within initialization has been terminated" << endl; return; }
  34.  
  35. int i = stack_ptr;
  36. if (i == MAX)
  37. {
  38. buf[i] = '\0';
  39. cout << buf << endl;
  40. }
  41. else
  42. {
  43. buf[i] = '0';
  44. ++stack_ptr; // since a destructor cannot take parameters
  45. hello_world::~hello_world();
  46. --stack_ptr;
  47. buf[i] = '1';
  48. ++stack_ptr;
  49. hello_world::~hello_world();
  50. --stack_ptr;
  51.  
  52. // Printing total number of calls at final call
  53. if (i == 0)
  54. cout << endl << "\"destrucotr_calls\" = " <<
  55. destructor_calls << endl;
  56. }
  57. }
  58.  
  59. void unlock()
  60. {
  61. init = true;
  62. }
  63. }; // end of class hello_world
  64.  
  65. int main()
  66. {
  67. char buf[MAX + 1];
  68. cout << "Calling hello_world class constructor..." << endl;
  69. hello_world h(buf);
  70. h.unlock();
  71. cout << "Calling hello_world class destructor..." << endl;
  72. return 0;
  73. }
  74.  
  75.  
  76. /*
  77. Output:
  78.  
  79. Calling hello_world class constructor...
  80. 000
  81. A destructor call within initialization has been terminated
  82. 001
  83. A destructor call within initialization has been terminated
  84. A destructor call within initialization has been terminated
  85. 010
  86. A destructor call within initialization has been terminated
  87. 011
  88. A destructor call within initialization has been terminated
  89. A destructor call within initialization has been terminated
  90. A destructor call within initialization has been terminated
  91. 100
  92. A destructor call within initialization has been terminated
  93. 101
  94. A destructor call within initialization has been terminated
  95. A destructor call within initialization has been terminated
  96. 110
  97. A destructor call within initialization has been terminated
  98. 111
  99. A destructor call within initialization has been terminated
  100. A destructor call within initialization has been terminated
  101. A destructor call within initialization has been terminated
  102. Calling hello_world class destructor...
  103. 000
  104. 001
  105. 010
  106. 011
  107. 100
  108. 101
  109. 110
  110. 111
  111.  
  112. "destrucotr_calls" = 15
  113. Press any key to continue . . .
  114.  
  115. */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In constructor ‘hello_world::hello_world(char* const&, int)’:
prog.cpp:9: warning: ‘hello_world::init’ will be initialized after
prog.cpp:8: warning:   ‘int hello_world::destructor_calls’
prog.cpp:13: warning:   when initialized here
prog.cpp: In destructor ‘hello_world::~hello_world()’:
prog.cpp:45: error: no matching function for call to ‘hello_world::~hello_world()’
prog.cpp:30: note: candidates are: hello_world::~hello_world()
prog.cpp:49: error: no matching function for call to ‘hello_world::~hello_world()’
prog.cpp:30: note: candidates are: hello_world::~hello_world()
stdout
Standard output is empty