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

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

      1. 如何使用 dynamic_cast 运算符识别失败的转换?

        How to identify failed casts using dynamic_cast operator?(如何使用 dynamic_cast 运算符识别失败的转换?)

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

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

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

                  本文介绍了如何使用 dynamic_cast 运算符识别失败的转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Scott Meyer 在他的 Effective C++ 一书中说 dynamic_cast 用于执行向下或跨继承层次结构的安全转换.也就是说,您使用 dynamic_cast 将指向基类对象的指针或引用转换为指向派生或同级基类对象的指针或引用,这样您就可以确定转换是否成功.

                  Scott Meyer in his book Effective C++ says dynamic_cast is used to perform safe casts down or across an inheritance hierarchy. That is, you use dynamic_cast to cast pointers or references to base class objects into pointers or references to derived or sibling base class objects in such a way that you can determine whether the casts succeeded.

                  失败的转换由空指针(转换指针时)或异常(转换引用时)指示.

                  Failed casts are indicated by a null pointer (when casting pointers) or an exception (when casting references).

                  我想得到两个代码片段,显示在转换指针和转换引用的情况下失败的转换.

                  I would like to get two code snippet showing the failed cast in the case of casting pointer and casting reference can be indicated.

                  推荐答案

                  对于指针,它是一个简单的空检查:

                  For pointers, it's a simple null check:

                  A* a = new A();
                  B* b = dynamic_cast<B*>(a);
                  
                  if (b == NULL)
                  {
                      // Cast failed
                  }
                  

                  对于参考,你可以抓住:

                  For references, you can catch:

                  try {
                      SomeType &item = dynamic_cast<SomeType&>(obj);
                  }
                  catch(const std::bad_cast& e) {
                      // Cast failed
                  }
                  

                  这篇关于如何使用 dynamic_cast 运算符识别失败的转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                  <legend id='NunCR'><style id='NunCR'><dir id='NunCR'><q id='NunCR'></q></dir></style></legend>

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

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