<tfoot id='EzMwh'></tfoot>

    <small id='EzMwh'></small><noframes id='EzMwh'>

  1. <i id='EzMwh'><tr id='EzMwh'><dt id='EzMwh'><q id='EzMwh'><span id='EzMwh'><b id='EzMwh'><form id='EzMwh'><ins id='EzMwh'></ins><ul id='EzMwh'></ul><sub id='EzMwh'></sub></form><legend id='EzMwh'></legend><bdo id='EzMwh'><pre id='EzMwh'><center id='EzMwh'></center></pre></bdo></b><th id='EzMwh'></th></span></q></dt></tr></i><div id='EzMwh'><tfoot id='EzMwh'></tfoot><dl id='EzMwh'><fieldset id='EzMwh'></fieldset></dl></div>

    <legend id='EzMwh'><style id='EzMwh'><dir id='EzMwh'><q id='EzMwh'></q></dir></style></legend>
      <bdo id='EzMwh'></bdo><ul id='EzMwh'></ul>
    1. exit() 在函数内部调用,该函数应该返回一个引用

      时间:2023-12-03

    2. <i id='KHuGE'><tr id='KHuGE'><dt id='KHuGE'><q id='KHuGE'><span id='KHuGE'><b id='KHuGE'><form id='KHuGE'><ins id='KHuGE'></ins><ul id='KHuGE'></ul><sub id='KHuGE'></sub></form><legend id='KHuGE'></legend><bdo id='KHuGE'><pre id='KHuGE'><center id='KHuGE'></center></pre></bdo></b><th id='KHuGE'></th></span></q></dt></tr></i><div id='KHuGE'><tfoot id='KHuGE'></tfoot><dl id='KHuGE'><fieldset id='KHuGE'></fieldset></dl></div>
    3. <legend id='KHuGE'><style id='KHuGE'><dir id='KHuGE'><q id='KHuGE'></q></dir></style></legend>
      1. <small id='KHuGE'></small><noframes id='KHuGE'>

            <bdo id='KHuGE'></bdo><ul id='KHuGE'></ul>
              <tbody id='KHuGE'></tbody>

                <tfoot id='KHuGE'></tfoot>
                本文介绍了exit() 在函数内部调用,该函数应该返回一个引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在一个库中,我有一个函数,它在数据库中搜索一个键并返回一个对对象的非常量引用.我想处理找不到key的情况,一般是调用函数时出错.这种情况非常糟糕,程序无法继续,所以我打印了一条消息来帮助发现错误并调用 exit(1).问题在于 return 语句在这种情况下永远不会执行,但无论如何都必须在那里.如果它是一个指针,我可以 return nullptr; 但是有一个引用?我应该做这样的伪代码吗?

                In a library I have a function which searches for a key in a database and return a non-const reference to an object. I want to handle the case in which the key is not found, which is typically caused by a mistake when calling the function. This situation is so bad that the program cannot continue, so I print a message to help to spot the bug and call exit(1). The problem is with the return statement which will never be executed in this case, but have to be there anyway. If it was a pointer I could just return nullptr; but with a reference? Should I do something like this pseudo code?

                 Type & get(const Key & k) {
                     if (my_db.key_exists(k)) {
                       return my_db.at(k);
                     }
                     std::cerr << k << " not found
                ";
                     exit(1); 
                     return *(new Type(some_dummy_parameters));
                 }
                

                看起来好可怕!也许我应该避免这样的功能.请让我知道您的意见!

                It looks so awful! Maybe I should just avoid such a function. Please, let me know your opinion!

                推荐答案

                这种情况很糟糕,程序无法继续,所以我打印了一条消息以帮助发现错误并调用 exit(1)

                This situation is so bad that the program cannot continue, so I print a message to help to spot the bug and call exit(1)

                没有.如果此代码是库的一部分,则库不应决定应用程序是否应退出.

                No. If this code is part of a library, the library should not be the one deciding if the application should exit or not.

                如果一个文件打开需要关闭,或者其他一些资源需要清理,或者你的数据库类的用户想要记录错误并继续做其他事情怎么办?

                What if a file is open and needs to be closed, or some other resource needs to be cleaned up, or if the user of your DB class wants to log the error and continue doing something else?

                答案是任何你现在在做什么.抛出异常、返回错误代码等,但不要在库或类代码中关闭应用程序.

                The answer is anything but what you're doing now. Throw an exception, return an error code, etc. but don't close the application down within library or class code.

                信不信由你,有一个商业数据库库可以完成你正在做的事情(关闭应用程序).他们从图书馆的用户那里得到了很多关于为什么他们意外关闭应用程序的愤怒回应.你知道吗——给客户的答案是我们觉得错误严重到足以停止应用程序,因为我们的库无法继续正常工作".这不仅是糟糕的推理,而且近乎傲慢,客户让他们知道这一点.

                Believe it or not, there was a commercial DB library that did exactly what you're doing (closing the app down). They got a lot of angry responses from users of their library as to why they're shutting the application down unexpectedly. And you know what -- the answer given to the customers was that "we felt the error was severe enough to stop the application, because our library can't continue to work properly". That is not only bad reasoning, it borders on arrogance, and the customers let them know that.

                这篇关于exit() 在函数内部调用,该函数应该返回一个引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:变量等末尾的与号 (&amp;) 下一篇:C++ 常量引用生存期(容器适配器)

                相关文章

                  <bdo id='DbHC6'></bdo><ul id='DbHC6'></ul>
                  <i id='DbHC6'><tr id='DbHC6'><dt id='DbHC6'><q id='DbHC6'><span id='DbHC6'><b id='DbHC6'><form id='DbHC6'><ins id='DbHC6'></ins><ul id='DbHC6'></ul><sub id='DbHC6'></sub></form><legend id='DbHC6'></legend><bdo id='DbHC6'><pre id='DbHC6'><center id='DbHC6'></center></pre></bdo></b><th id='DbHC6'></th></span></q></dt></tr></i><div id='DbHC6'><tfoot id='DbHC6'></tfoot><dl id='DbHC6'><fieldset id='DbHC6'></fieldset></dl></div>
                  <tfoot id='DbHC6'></tfoot>
                  1. <legend id='DbHC6'><style id='DbHC6'><dir id='DbHC6'><q id='DbHC6'></q></dir></style></legend>
                  2. <small id='DbHC6'></small><noframes id='DbHC6'>