从用户定义的函数转到 main()
我有 3 个函数:my_fun1()
、my_fun2()
和 my_fun3()
。
main()
调用 my_fun1()
,my_fun1()
又调用 my_fun2()
,后者又调用 my_fun3()
。
根据 my_fun3()
中的某些预定义条件,我希望我的程序直接返回到 my_fun1()< 行处的
main()
函数/code> 被调用。
是否可以直接从 my_fun3()
转到 main()
,或者我是否必须向 my_fun2()
和 < code>my_fun1() 来实现这个?
I have 3 functions: my_fun1()
, my_fun2()
, and my_fun3()
.
main()
calls my_fun1()
which calls my_fun2()
, which in turn calls my_fun3()
.
Upon some pre-defined condition in my_fun3()
, I would like my program to go directly back to the main()
function at the line my_fun1()
was called.
Is it possible to go directly from my_fun3()
to main()
, or do I have to add some conditions to my_fun2()
and my_fun1()
to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不滥用异常的干净方法是
A clean approach without abusing exceptions would be
看来您可能暗示
goto
这是不好的。没有看到程序的结构,您可以考虑在从函数 #3 返回时检查返回布尔值(例如标志),该值在 #2 中检查,然后返回,在 #1 中检查,然后返回到
main()
。It seems you may be hinting at a
goto
which is bad.Not seeing the structure of your program, you may consider a return boolean value (e.g. a flag) that is checked upon returning from function #3, which is checked in #2, which then returns, checked in #1, and then returns to
main()
.可以通过抛出异常来实现,就像 larsmans 所说的那样(尽管仅限于 C++)。也可以通过 setjmp()/longjmp() 来实现,它充当一种跨函数 goto 语句。
但如果这确实是您想要/需要的设计,那么您的设计可能有问题。 my_fun1 和 my_fun2 中的条件可能是这里的“正确”答案,但这当然取决于您在做什么。如果您发布更多关于为什么您想要/需要这个的信息,我们也许能够更好地帮助您。
It's possible by throwing an exception, like larsmans said (though C++ only). It's also possible via setjmp()/longjmp(), which acts as a sort of cross-function goto statement.
But if this is really the design you want/need, then there's probably something wrong with your design. Having conditions in my_fun1 and my_fun2 may be the "correct" answer here, but it depends on what you're doing, of course. If you post a bit more information on why you may want/need this, we may be able to help you better.
只需将您的第一个函数调用放入循环中即可。无论如何,这是普遍接受的编程方法之一。
所以你可以做
如果你需要 function1() 和 function2() 中止执行基于此行为的某些操作,那么你的情况要么是一个错误,要么基本上就像一个错误,所以抛出异常将是一个更好的解决方案。即使不是错误,也基本上是同一类型的控制逻辑。
但是,如果这是程序的非错误控制流,您可能需要更多地查看结构并找出导致您需要这样做的原因,并尝试避免导致这种情况的任何逻辑。
Just put your first function call in a loop. That is one of the generally accepted programming methodologies anyway.
So you can do
If you need function1() and function2() to abort doing something based on this behavior, then your situation either is an error or is basically like an error, so throwing an exception would then be a better solution. Even if it's not an error, it's still basically the same type of control logic.
However, if this is the non-error control flow of your program, you might want to look at the structure more and figure out what is causing you to need to do it this way and try to avoid whatever logic is causing this.
好吧,有一种方法可以使用
longjmp
/setjmp
直接跳转到代码中的另一点,但我不会告诉您如何操作,因为它是一个 可怕的想法。就和它得到的一样糟糕。那么让我们来谈谈好的解决方案:-)。最明显的方法是使用异常。像这样:
如果您不想使用异常,(可能是这种情况,有些人可能会认为,如果返回主的原因不是“异常”,那么这将是对异常的滥用") 下一个最直接的方法是让
my_fun1
、my_fun2
和my_fun3
返回表示“完成”的值,比方说int
其中小于0
的值表示“返回 main”。您的调用结构将如下所示:Well, there is a way to jump directly to another point in the code using
longjmp
/setjmp
, but I'm not going to tell you how because it is a terrible idea. Just about as bad as it gets. So let's talk about good solutions :-).The most obvious way is to use exceptions. Like this:
If you don't want to use exceptions, (which may be the case, some may argue that this would be an abuse of exceptions if the reason for going back to main is not "exceptional") the next most straightforward way is to make
my_fun1
,my_fun2
, andmy_fun3
return values meaning "done", let's say anint
where a value less than0
means "go back to main". Your call structure would look like this:是的,可以通过抛出异常并在
main
中捕获该异常来实现。但请不要使用异常作为一般的控制流机制,这不是它们的用途。还有
setjmp
/longjmp
,但使用它们的代码很难理解。Yes, it's possible by throwing an exception and catching that in
main
. But please don't use exceptions as a general control flow mechanism, that's not what they're for.There's also
setjmp
/longjmp
, but code using them is very hard to follow.要么从所有函数返回,要么抛出并捕获异常(不推荐,除非向上堆栈的原因确实是错误情况)
Either return from all functions or throw and catch an exception (not recommended unless the reason to go up the stack is really error situation)