【问题标题】:C++ wrap multiple returnsC++ 包装多个返回
【发布时间】:2016-09-21 09:52:31
【问题描述】:

我有以下代码在多行中返回错误:

bool func()
{
    if (acondition)
    {
        return 0;
    }
    return 1;
}

int cmdfun()
{
    other_funcs;
    if (func()) return ERROR#NUMBER;
    other_funcs;
    if (func()) return ERROR#NUMBER;
}

但我发现它变得越来越长。如何将 return ERROR#NUMBER 也封装到 func() 中?或者任何封装 if (func()) return ERROR;进入另一个独立的函数?

【问题讨论】:

  • 为什么不使用为这种情况设计的异常?
  • @πάντα ῥεῖ 就是像func()->cmdfun()->main()这样封装调用栈的返回,这样func()就可以直接返回ERROR给main(),所以有将无需在 cmdfun() 中输入 return ERROR。
  • 你不能encapsulate return改用上面提到的异常。
  • @ πάντα ῥεῖ 但这也不例外。它只是双重回报和封装回报。
  • 好吧,如果你一直拒绝接受帮助,我们该怎么办?

标签: c++ function return


【解决方案1】:

单独使用return 无法真正实现这一点。

但是你可以throw 中的一个异常func 会冒泡调用堆栈,就像你希望程序控制那样:

struct myexception{}; /*ToDo - inherit from std::exception?*/
bool func()
{
    if (acondition){
        return 0; /*normal behaviour, perhaps make `func` void if not needed?*/
    }
    throw myexception();
}

cmdfun 然后采用以下形式:

int cmdfun()
{
    other_funcs;
    func();
    other_funcs;
    func();
    /* don't forget to return something*/
}

最后,请确保您在catch 的调用者中将异常设置为cmdfun

【讨论】:

    【解决方案2】:

    正如我所说,它不是异常,不能由 std::exception 处理,它只是一条错误消息,而 ERROR#NUMBER 只是另一个宏。而且我无法访问 cmdfun() 的调用者。所以无法采用第一个答案。但是在问了别人之后,可以封装返回并在输入时节省时间,虽然不推荐,但在这种特殊情况下,我可以使用宏。下面给出一个完整的例子:

    #include <iostream>
    using namespace std;
    
    #define CHECK_VEC(acondition)\
    if(checkcondition(acondition)) return -1;
    
    bool checkcondition(bool acondition)
    {
        if (acondition) return 1;
        return 0;
    }
    
    int fun_called_by_main()
    {
        int a = 5 + 4;
        bool acondition = a;
        CHECK_VEC(acondition);
    
        return 1;
    }
    
    
    int main()
    {
        int a = fun_called_by_main();
    
        cout << a << endl;
    
        cin.get();
        return 0;
    }
    

    【讨论】:

      【解决方案3】:

      如果我正确理解了您的问题,您是在为您自己的错误要求一个“错误报告者”。对于 2 种不同的情况,有 2 种解决方案:

      案例 1 - 您仍想使用 return 语句来制作“错误报告者”:

      为此,您必须创建另一个函数或学习如何使用 goto。但是,您不需要 - 您的函数返回一个 boolean(bool) - 这意味着您只有 2 个可能的结果:0 (False) 和 1 (True)

      bool func()
      {
          if (acondition)
          {
              return (bool)0; // False (no error)
          }
          return (bool)1; // True (error)
          // Note: I used (bool)0 and (bool)1 because it is 
          // more correct because your returning type is bool.
      }
      
      void errorcase(bool trueorfalse)
      {
          switch(trueorfalse)
          {
          case False:
              ... // your code (func() returned 0)
              break;
          default:
              ... // your code (func() returned 1)
              break;
      
              // Note that you will not need to check if an error occurred every time.
          }
          return;
      }
      
      int cmdfun()
      {
          ... // your code
          errorcase(func());
          ... // again - your code
          return 0; // I suppouse that you will return 0... 
      }
      

      但我认为第二种情况更有趣(不幸的是,作为初学者也很难理解,第一种解决方案对您来说可能更容易):

      案例 2 - 你决定以其他方式做这件事 - 那是通过学习 throw 和 catch - 我不会重复答案,因为它已经给出:@Bathsheba 回答 preety good...

      【讨论】:

      • 基本上我想在名为 cmdfun() 的函数中返回 ERROR,例如main(),来自cmdfun()直接调用的函数,这里是func()。所以栈是func()->cmdfun()->main(),但是我希望func()是直接返回ERROR给main()的那个。
      • @VVI C 和 C 15 不要让 OP 继续找错树。你的回答没有多大价值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      相关资源
      最近更新 更多