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

      1. 为什么要捕获异常作为对 const 的引用?

        时间:2023-08-03

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

          <legend id='m7U08'><style id='m7U08'><dir id='m7U08'><q id='m7U08'></q></dir></style></legend>
            • <bdo id='m7U08'></bdo><ul id='m7U08'></ul>
                <tbody id='m7U08'></tbody>

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

                  本文介绍了为什么要捕获异常作为对 const 的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我多次听说并阅读过,最好将异常作为对常量的引用而不是作为引用来捕获.为什么是:

                  I've heard and read many times that it is better to catch an exception as reference-to-const rather than as reference. Why is:

                  try {
                      // stuff
                  } catch (const std::exception& e) {
                      // stuff
                  }
                  

                  优于:

                  try {
                      // stuff
                  } catch (std::exception& e) {
                      // stuff
                  }
                  

                  推荐答案

                  您需要:

                  • 一个引用,以便您可以多态地访问异常
                  • 用于提高性能的常量,并告诉编译器您不会修改对象

                  后者不如前者重要,但放弃 const 的唯一真正原因是表明您想要对异常进行更改(通常只有在您想要更改时才有用)将添加的上下文重新抛出到更高级别).

                  The latter is not as much important as the former, but the only real reason to drop const would be to signal that you want to do changes to the exception (usually useful only if you want to rethrow it with added context into a higher level).

                  这篇关于为什么要捕获异常作为对 const 的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我应该何时以及如何使用异常处理? 下一篇:当没有异常时,C++ 异常会以何种方式减慢代码速

                  相关文章

                  最新文章

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

                    <legend id='Ejhlu'><style id='Ejhlu'><dir id='Ejhlu'><q id='Ejhlu'></q></dir></style></legend>
                    • <bdo id='Ejhlu'></bdo><ul id='Ejhlu'></ul>
                  1. <small id='Ejhlu'></small><noframes id='Ejhlu'>

                    1. <tfoot id='Ejhlu'></tfoot>