簡體   English   中英

如何在結束 C++ 命令行應用程序時運行函數?

[英]How to run function on ending a C++ Command Line App?

我知道這可能已經在這里有了答案,但基本上我需要在強制關閉命令行時關閉服務器上的會話。

使用 C++ 析構函數。 在 main() 的開頭,定義一個帶有會話類的變量,如下所示。

class ManageSession
{
public:
   int sessionId;
   ManageSession() {
      sessionId = Connect();
   }
   ~ManageSession()
   {
      Close(sessionId);
   }

   int Connect() {
      // find and connect
      return 42;
   }
   void Close(int id) {
      if (id > 0) {
         id = 0; // close it
      }
   }
};

int main() {
   ManageSession session;

   // do amazing things
};

暫無
暫無

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

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