簡體   English   中英

如何捕獲c ++命令行項目的應用程序關閉事件?

[英]How to capture application close event for c++ command line project?

我正在開發visual studio中的c ++命令行項目。 我需要捕獲應用程序關閉事件(在運行應用程序時使用CTRL + Z關閉應用程序或關閉命令行窗口)。

我的應用看起來像:

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

       getchar();
    }

如果我如上所述顯式關閉應用程序,myApp析構函數將不會執行。 所以我需要一些方法來捕獲我的應用程序中的結束事件(如Qt中的QObject :: closeEvent())。

提前致謝。

在通常情況下,析構函數不會導致在main()創建的對象。 但是有一個很好的和優雅的解決方案 - 你可以將你的代碼放入額外的括號中 ,然后調用析構函數:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM