开发者

destructor does not work?

#include <iostream>

using namespace std;

class Tester {
public:
Tester(int x);
~Tester();
int who;
} Tester_g_1(1) , Tester_g_2(2);

Tester::Tester(int id) {
cout << "Intializing" << id << endl ;
who = id;
}

Tester::~Tester() {
cout << "Destroying" << who << endl;
}

int main() {
Tester localObj(3);
cout << "This is not the first line to be displayed";
    system("pause");
return 0;

}

The output that i get is:

Intializing1
Intializing2
Intializing3
This is not the first line to be displayedPress any key to continue . . .

Why does the statement in destructor dodes not work? In use : Compiler - microsoft visual c++ 2010 开发者_C百科Express OS - Win7


localObj is destructed when the main terminates (because its it's scope), which happens after the system("pause"). You press a key, the destructor runs but immediately the window closes, so you don't see it.

To see the text of the destructor, you have to run the program from the command line, or use the "Start program without debugging" item from the Run menu (IIRC the VS menus) (the hotkey for it is Ctrl+F5 - thanks @Cody Gray). This adds a "Press any key to continue" after the executable has terminated, so you'll be able to see the text wrote by the destructor.

Another way to see the destructor run can be enclosing the variable in a smaller scope, which you can do easily like this:

// ...

int main() {
    {    // the braces create a new scope...
        Tester localObj(3);
        cout<<"Inside the Tester scope..."<<endl;
    }    // ... that ends here
    cout << "... outside the Tester scope!";
    cout<<"Press Enter to exit...";
    cin.ignore();
    return 0;
}

By the way, system("pause") is ugly and non-portable; you should avoid it.


Destructors have called after Press any key to continue . . .


The variable is destroyed at the end of the scope.

To it is actually destroyed AFTER the pause, so you probably just don't see it.

If you'll run it in the Command Prompt, you'll see the line.


At the point your pause runs the object hasn't gone out of scope yet, so its destructor isn't called. Instead of using pause, just run your program from the command line so you can see the full output and you'll see that it's called.


Your object is not out of scope yet. You are still hanging around at the system call.

You can introduce new scope with a pair of braces:

{
   Tester localObj(3);
   cout << "This will be run before the destructor." << endl;
}

or run the program in a command line prompt (run cmd.exe, then start the program from the prompt.), which gives you time enough to see the output from your destructor.


Because you are not destroying the object. As long as it remains in scope, there is no reason why the destructor should be called.

Try this instead:

void someOtherFunction(){
Tester localObj(3);
}

int main() {
someOtherFunction();
cout << "This is not the first line to be displayed";
    system("pause");
return 0;

}

Or you might also allocate the object on the heap (as opposed to using the stack as in the example above):

Tester* tester=new Tester(3);
delete tester;


Objects created on the stack are destroyed automatically when they go out of the scope. To prove this for your local variable, you could have used dummy scope within your main():

int main() 
{
   {
      Tester localObj(3);
   }

   cout << "This is not the first line to be displayed";
   system("pause");
   return 0;
}

Global variables you instantiated on the stack are destroyed just after main() returns (but before std::cout is destroyed).


chances are it does show, but you don't have enough time to see it happen(the destruction would happen during the "return 0;" statement".

Prehapse if you were to put that code into a function, you'd be able to see the destruction of localObj,since it'd fall out of scope after you leave the function:

void f(){
    Tester localObj(3);
    cout<<"In local function f";
}

/*...*/ 

int main(){
    f();
    system("pause");
    return 0;
}


Well your destructor is being called after the

return 0;

Hence you didn't see what destructor has done. But it still runs, you just can't see it.

If you are in VStudio you can press CTRL+F5, it will add "Press any key to continue" after your program is terminated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜