【发布时间】:2015-12-24 04:32:15
【问题描述】:
一般说我有一些函数step1step2...它们被依次调用:
int main()
{
...
step1(); // something wrong detected and need to break out of the whole program
step2();
step3();
...
}
我怎样才能脱离step1 并跳过所有其余代码来终止main() 函数?
目前我只能想到将像bool isErr这样的全局变量设置为标志,以便
step1(); // error detected and isErr is set to 1 inside step1()
if (isErr)
return;
step2();
...
是否有更好或更“规范”的方法?
顺便说一句,我听说goto 不好,所以我放弃了它:)
【问题讨论】:
-
How to quit a C++ program? 的可能重复项
-
如果问题是由于您希望用户能够检查的某些数据流,
std::abort()可能是一个好主意,因为它会以信号退出进程并可能导致主机创建核心转储(使情况可通过多种方式调试)。 -
C 不是 C++ 不是 C!选一个!