quick_exit
C ++ at_quick_exit()函数 (C++ at_quick_exit() function)
at_quick_exit() function is a library function of cstdlib header. It is used to set a function that should be executed on quick exit. Sometimes we need to write such code (like freeing the memory occupied by the global variables, closing the file objects, etc) that should be executed when the quick exit, any user-defined can be written for it and that can be set as a quick exit function using using at_quick_exit() function.
at_quick_exit()函数是cstdlib标头的库函数。 它用于设置应在快速退出时执行的功能。 有时我们需要编写这样的代码(例如释放全局变量占用的内存,关闭文件对象等),该代码应在快速退出时执行,可以为其编写任何用户定义的代码,并将其设置为使用at_quick_exit()函数的快速退出功能 。
Syntax of at_quick_exit() function:
at_quick_exit()函数的语法:
C++11:
C ++ 11:
extern "C" int at_quick_exit (void (*func)(void)) noexcept;
extern "C++" int at_quick_exit (void (*func)(void)) noexcept;
Parameter(s):
参数:
func – represents the function to be called on quick exit.
func –表示快速退出时要调用的函数。
Return value:
返回值:
The return type of this function is int, it returns 0 if the function registered successfully; non-zero, otherwise.
该函数的返回类型为int ,如果该函数成功注册,则返回0;否则返回0。 非零,否则。
Example:
例:
// defining the function
void function_name(void){
// function code
}
// setting the function
at_quick_exit(function_name);
C ++代码演示at_quick_exit()函数的示例 (C++ code to demonstrate the example of at_quick_exit() function)
// C++ code to demonstrate the example of
// at_quick_exit() function
#include <iostream>
#include <cstdlib>
using namespace std;
void myfunc(void)
{
cout << "Bye, Bye..." << endl;
}
// main() section
int main()
{
//setting the functions
at_quick_exit(myfunc);
cout << "Hi, friends..." << endl;
cout << "Input first number: ";
int a;
cin >> a;
cout << "Input second number: ";
int b;
cin >> b;
cout << a << "+" << b << " = " << a + b << endl;
//quick exit
quick_exit(EXIT_SUCCESS);
cout << "End of the main()..." << endl;
return 0;
}
Output
输出量
Hi, friends...
Input first number: 10
Input second number: 20
10+20 = 30
Bye, Bye...
Reference: C++ at_quick_exit() function
翻译自: https://www.includehelp.com/cpp-tutorial/at_quick_exit-function-with-example.aspx
quick_exit