从用户定义的函数转到 main()

发布于 2024-11-24 08:08:23 字数 473 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

孤千羽 2024-12-01 08:08:25

不滥用异常的干净方法是

#define EXITCONDITION 42

void main ()
{
  ...
  myfunc1 ();
  ...
}

int myfunc1 ()
{
  ...
  if (myfunc2 () == EXITCONDITION) return EXITCONDITION;
  ...
}

int myfunc2 ()
{
  ...
  if (myfunc3 () == EXITCONDITION) return EXITCONDITION;
  ...
}

int myfunc3 ()
{
  ...
  if (somethingweirdhappens) retrun EXITCONDITION;
  ...
}

A clean approach without abusing exceptions would be

#define EXITCONDITION 42

void main ()
{
  ...
  myfunc1 ();
  ...
}

int myfunc1 ()
{
  ...
  if (myfunc2 () == EXITCONDITION) return EXITCONDITION;
  ...
}

int myfunc2 ()
{
  ...
  if (myfunc3 () == EXITCONDITION) return EXITCONDITION;
  ...
}

int myfunc3 ()
{
  ...
  if (somethingweirdhappens) retrun EXITCONDITION;
  ...
}
浮世清欢 2024-12-01 08:08:25

看来您可能暗示 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().

蓝戈者 2024-12-01 08:08:25

可以通过抛出异常来实现,就像 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.

゛清羽墨安 2024-12-01 08:08:25

只需将您的第一个函数调用放入循环中即可。无论如何,这是普遍接受的编程方法之一。

main
    while running
        do stuff

所以你可以做

bool running = false;

int main(int argc, char **argv)
{
    while(running)
        function1();
}

void function3()
{
    ...
    if(whatever)
    {
        running = true;
        return;
    }
    ...
}

如果你需要 function1() 和 function2() 中止执行基于此行为的某些操作,那么你的情况要么是一个错误,要么基本上就像一个错误,所以抛出异常将是一个更好的解决方案。即使不是错误,也基本上是同一类型的控制逻辑。

但是,如果这是程序的非错误控制流,您可能需要更多地查看结构并找出导致您需要这样做的原因,并尝试避免导致这种情况的任何逻辑。

Just put your first function call in a loop. That is one of the generally accepted programming methodologies anyway.

main
    while running
        do stuff

So you can do

bool running = false;

int main(int argc, char **argv)
{
    while(running)
        function1();
}

void function3()
{
    ...
    if(whatever)
    {
        running = true;
        return;
    }
    ...
}

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.

你怎么敢 2024-12-01 08:08:24

好吧,有一种方法可以使用 longjmp/setjmp 直接跳转到代码中的另一点,但我不会告诉您如何操作,因为它是一个 可怕的想法。就和它得到的一样糟糕。那么让我们来谈谈好的解决方案:-)。

最明显的方法是使用异常。像这样:

int my_fun3() {
    throw 1; // could be any type...
}
int my_fun2() {
    my_fun3();
}

int my_fun1() {
    my_fun2();
}

int main() {
    try {
        my_fun1();
    } catch(int n) { // catch the same type you threw...
    }
}

如果您不想使用异常,(可能是这种情况,有些人可能会认为,如果返回主的原因不是“异常”,那么这将是对异常的滥用") 下一个最直接的方法是让 my_fun1my_fun2my_fun3 返回表示“完成”的值,比方说int 其中小于 0 的值表示“返回 main”。您的调用结构将如下所示:

int my_fun3() {
   // ...
   if(some_condition) {
       return -1;
    }

    return 0;
}

int my_fun2() {
    // ...
    int r = my_fun3();
    if(r < 0) {
        return r;
    }
    // ...
    return 0;
}

int my_fun1() {
    // ...
    int r = my_fun2();
    if(r < 0) {
        return r;
    }
    // ...
    return 0;
}

int main() {
    my_fun1();
}

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:

int my_fun3() {
    throw 1; // could be any type...
}
int my_fun2() {
    my_fun3();
}

int my_fun1() {
    my_fun2();
}

int main() {
    try {
        my_fun1();
    } catch(int n) { // catch the same type you threw...
    }
}

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, and my_fun3 return values meaning "done", let's say an int where a value less than 0 means "go back to main". Your call structure would look like this:

int my_fun3() {
   // ...
   if(some_condition) {
       return -1;
    }

    return 0;
}

int my_fun2() {
    // ...
    int r = my_fun3();
    if(r < 0) {
        return r;
    }
    // ...
    return 0;
}

int my_fun1() {
    // ...
    int r = my_fun2();
    if(r < 0) {
        return r;
    }
    // ...
    return 0;
}

int main() {
    my_fun1();
}
甩你一脸翔 2024-12-01 08:08:24

是的,可以通过抛出异常并在 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.

数理化全能战士 2024-12-01 08:08:24

要么从所有函数返回,要么抛出并捕获异常(不推荐,除非向上堆栈的原因确实是错误情况)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文