设置终止和意外处理程序
Setting the Terminate and Unexpected Handler
你能解释一下吗:
terminate_handler set_terminate (terminate_handler f) throw();
还有这个:
unexpected_handler set_unexpected (unexpected_handler f) throw();
要更改我们使用的终止处理程序,必须使用上面显示的 set_terminate()
,但我不能 understand/interpret 以上形式。谁能解释一下。
我也很难理解:
terminate_handler set_terminate (terminate_handler f) throw();
Here,
f is a pointer to the new terminate handler.The function
returns a pointer to the old terminate handler. The new terminate
handler must be of type terminate_handler, which is defined like this:
typedef void(*terminate_handler)();
terminate_handler
是函数指针的类型定义。当您设置终止处理程序时,您将一个指针传递给您希望在终止时调用的函数。这就是 set_terminate
的参数。函数returns旧指针。这样,如果您只想在短时间内使用自己的终止处理程序,您可以在完成后恢复之前的处理程序:
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;
}
你能解释一下吗:
terminate_handler set_terminate (terminate_handler f) throw();
还有这个:
unexpected_handler set_unexpected (unexpected_handler f) throw();
要更改我们使用的终止处理程序,必须使用上面显示的 set_terminate()
,但我不能 understand/interpret 以上形式。谁能解释一下。
我也很难理解:
terminate_handler set_terminate (terminate_handler f) throw();
Here, f is a pointer to the new terminate handler.The function returns a pointer to the old terminate handler. The new terminate handler must be of type terminate_handler, which is defined like this:
typedef void(*terminate_handler)();
terminate_handler
是函数指针的类型定义。当您设置终止处理程序时,您将一个指针传递给您希望在终止时调用的函数。这就是 set_terminate
的参数。函数returns旧指针。这样,如果您只想在短时间内使用自己的终止处理程序,您可以在完成后恢复之前的处理程序:
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;
}