• <legend id='I2V0U'><style id='I2V0U'><dir id='I2V0U'><q id='I2V0U'></q></dir></style></legend>

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

      • <bdo id='I2V0U'></bdo><ul id='I2V0U'></ul>

        <tfoot id='I2V0U'></tfoot>
        <i id='I2V0U'><tr id='I2V0U'><dt id='I2V0U'><q id='I2V0U'><span id='I2V0U'><b id='I2V0U'><form id='I2V0U'><ins id='I2V0U'></ins><ul id='I2V0U'></ul><sub id='I2V0U'></sub></form><legend id='I2V0U'></legend><bdo id='I2V0U'><pre id='I2V0U'><center id='I2V0U'></center></pre></bdo></b><th id='I2V0U'></th></span></q></dt></tr></i><div id='I2V0U'><tfoot id='I2V0U'></tfoot><dl id='I2V0U'><fieldset id='I2V0U'></fieldset></dl></div>
      1. C++中通过指针捕获异常

        时间:2023-08-03

            <legend id='uvCce'><style id='uvCce'><dir id='uvCce'><q id='uvCce'></q></dir></style></legend>
              <tbody id='uvCce'></tbody>

                <bdo id='uvCce'></bdo><ul id='uvCce'></ul>
                <tfoot id='uvCce'></tfoot>

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

                  本文介绍了C++中通过指针捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我发现有3种方法可以捕获异常,有什么区别?

                  I found that there are three ways to catch an exception, what are the differences?

                  1) 按值捕获;

                  2) 按引用捕获;

                  3) 通过指针捕捉;

                  我只知道按值捕获会调用对象的两个副本,按引用捕获会调用一个.那么如何通过指针捕获呢?什么时候用指针捕捉?除了抛出一个对象,我还能像这样抛出一个指向对象的指针吗?

                  I only know that catch by value will invoke two copies of the object, catch by reference will invoke one. So how about catch by pointer? When to use catch by pointer? In addition to throw an object, can I throw a pointer to an object like this?

                  class A {}
                  
                  void f() {
                  
                    A *p = new A();
                          throw p;
                  
                  
                  }
                  

                  推荐答案

                  推荐的方式是按值抛出,按引用捕获.

                  您的示例代码抛出一个指针,这是一个坏主意,因为您必须在捕获站点管理内存.

                  Your example code throws a pointer, which is a bad idea since you would have to manage memory at the catch site.

                  如果你真的觉得你应该抛出一个指针,使用智能指针,比如 shared_ptr.

                  If you really feel you should throw a pointer, use a smart pointer such as shared_ptr.

                  无论如何,Herb Sutter 和 Alexei Alexandrescu 在我转述的他们的 C++ 编码标准一书中很好地解释了这一点.

                  请参阅 C++ 编码标准:按值抛出,按引用捕获.

                  这篇关于C++中通过指针捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我什么时候应该真正使用 noexcept? 下一篇:C++中的对象销毁

                  相关文章

                  最新文章

                    <tfoot id='qJHYz'></tfoot>
                  1. <legend id='qJHYz'><style id='qJHYz'><dir id='qJHYz'><q id='qJHYz'></q></dir></style></legend>
                    • <bdo id='qJHYz'></bdo><ul id='qJHYz'></ul>

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

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