Table Of Contents
Previous Section Next Section

Program 83: Wild Output

A student of C++ wanted to see how constructors and destructors were called, so he wrote the following program. Yet he learned more than he bargained for. What's the problem?

  1 /************************************************
  2  * Class tester.   Test constructor / destructor*
  3  *      calling.                                *
  4  ************************************************/
  5 #include <iostream>
  6
  7 /************************************************
  8  * tester -- Class that tells the world when    *
  9  *      it's created and destroyed.             *
 10  ************************************************/
 11 class tester {
 12     public:
 13         tester(void) {
 14             std::cout <<
 15                 "tester::tester() called\n";
 16         }
 17         ~tester(void) {
 18             std::cout <<
 19                 "tester::~tester() called\n";
 20         }
 21 };
 22
 23 static tester a_var;    // Variable to test with
 24
 25 int main()
 26 {
 27     std::cout << "In main\n";
 28     return (0);
 29 }

(Next Hint 157. Answer 111.)

Table Of Contents
Previous Section Next Section