Destructors not called?
The output is:
Destructor object1( 1, "global object" );
cout << "\n\nENTER MAIN FUNCTION\n";
Destructor object2( 2, "local automatic" );
static Destructor object3( 3, "static object" );
cout << "\nEXIT MAIN FUNCTION\n";
cout << "\nCREATE FUNCTION IS CALLED" << endl;
Destructor object4( 4, "local automatic for create function" );
static Destructor object5( 5, "static object for create function" );
cout << "\nEXIT CREATE FUNCTION\n";
Destructors are called automatically when an object goes out of scope, I understand that the destructors are called in reverse order when the objects are created. The code above is provided by the book, the output from the book is showing that all the objects created are destructed ( displaying Object blah..blah..blah is destructed) but as you can see in my output the only object destroucted that I can explicitly see is the Object 4. Where are the others?
Object 1 global object is created
Object 2 local automatic is created
Object 3 static object is created
CREATE FUNCTION IS CALLED
Object 4 local automatic for create function is created
Object 5 static object for create function is created
EXIT CREATE FUNCTION
Object 4 local automatic for create function is destructed
EXIT MAIN FUNCTION
It gets destroyed when you hit the end of main (after your cin.get()) - are you actually running your code from a command prompt, or running from an IDE that closes the windom immediately after it finishes - if so, you may not SEE the destructor call's printout.
If that output which you captured is while your program is waiting on the cin.get(), then the output is correct. Object 2 will be destructed at the end of main, and I believe all static objects are destructed at the same time, if not then sometime after main ends but before the process is cleared out. Try removing the cin.get(), recompiling, then running the program from an open console window so it does not close when the program exits...
lol, I never thought of that.. such a small problem caused a lot of confusion.. Thanks everybody..
Originally Posted by matsp
I'm using DEVC++, I understand now what happened. I tried to run the pogram in a command prompt and I saw the result.. thanks so much to you all.