How do I capture the application close event for a C ++ command line project?

I am developing a C ++ command line project in visual studio. I need to capture the application close event (closing the application with CTRL + Z or closing the command prompt window when the application starts).

My application looks like this:

  int main()
    {
       //crete app object, will open some files and run the code.
       myApp app;
       app.run();

       getchar();
    }

      

If I close the application explicitly, as mentioned above, the myApp destructor won't execute. so I need to somehow capture this closing event in my application (like QObject :: closeEvent () in Qt).

Thanks in advance.

+3


source to share


1 answer


Normally, the destructor is not called for objects created in . But there is a nice and elegant solution - you can put your code in extra parentheses and then the destructor is called: main()



int main()
{
    {        
        myApp app;
        app.run();

        getchar();
    } // At this point, the object destructor is invoked
}

      

0


source







All Articles