set terminate function c++ [duplicate]
terminate_handler
is a typedef for a function pointer. When you set the terminate handler, you pass a pointer to the function that you want to be called on termination. That’s the argument to set_terminate
. The function returns the old pointer. That way, if you just want to use your own terminate handler for a short period of time, you can restore the previous one when you’re done:
void my_terminator() {
// whatever
}
int main() {
// terminate here calls default handler
terminate_handler old_handler = set_terminate(my_terminator);
// now, terminate will call `my_terminator`
set_terminate(old_handler);
// now, terminate will call the default handler
return 0;
}