你能解释一下吗:
terminate_handler set_terminate (terminate_handler f) throw();
还有这个:
unexpected_handler set_unexpected (unexpected_handler f) throw();
要更改我们使用的终止处理程序,必须使用 set_terminate()
,如上所示,但我无法理解/解释上面的形式。谁能解释一下。
我也很难理解这一点:
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
的参数。该函数返回旧指针。这样,如果您只想在短时间内使用自己的终止处理程序,则可以在完成后恢复前一个:
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;
}
关于c++ - 设置终止和意外处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47110343/