<tfoot id='vf2VE'></tfoot>

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

    1. <small id='vf2VE'></small><noframes id='vf2VE'>

        <bdo id='vf2VE'></bdo><ul id='vf2VE'></ul>
    2. C++ 什么时候我们应该更喜欢使用两个链接的 static_cast 而不是 reinterpret_cast

      时间:2023-09-26

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

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

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

          1. <tfoot id='kBMBu'></tfoot><legend id='kBMBu'><style id='kBMBu'><dir id='kBMBu'><q id='kBMBu'></q></dir></style></legend>

                  <tbody id='kBMBu'></tbody>
                本文介绍了C++ 什么时候我们应该更喜欢使用两个链接的 static_cast 而不是 reinterpret_cast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                首先,这不是 为什么我们要在 C++ 中使用 reinterpret_cast,而两个链接的 static_cast 可以完成它的工作?.

                我知道我们甚至不能使用两个链接的 static_cast 来实现这一点的情况,reinterpret_cast 可以做什么.但是在任何情况下,我应该更喜欢两个链接的 static_cast 而不是简单且更具可读性的 reinterpret_cast?

                I know situations where we cannot use even two chained static_cast to achieve that, what reinterpret_cast does. But is there any situation where I should prefer a two chained static_cast over a simple and more readable reinterpret_cast?

                推荐答案

                reinterpret_cast 应该是一个巨大的闪烁符号,表示这看起来很疯狂,但我知道我在做什么.不要因为懒惰而使用它.

                reinterpret_cast should be a huge flashing symbol that says THIS LOOKS CRAZY BUT I KNOW WHAT I'M DOING. Don't use it just out of laziness.

                reinterpret_cast 表示将这些位视为……" 链式静态转换相同,因为它们可能会根据继承晶格修改其目标.

                reinterpret_cast means "treat these bits as ..." Chained static casts are not the same because they may modify their targets according to the inheritence lattice.

                struct A {
                    int x;
                };
                
                struct B {
                    int y;
                };
                
                struct C : A, B {
                    int z;
                };
                
                C c;
                A * a = &c;
                
                int main () {
                    assert (reinterpret_cast <B *> (a) != static_cast <B *> (static_cast <C *> (a)));
                }
                

                如果您不是 100% 确定 a 指向 b,请使用 dynamic_cast 它将搜索上述解决方案(尽管使用运行时成本).请记住,这可能会返回 NULL 或引发失败.

                If you are not 100% sure that a points to a b, use dynamic_cast which will search for the above solution (albeit with a runtime cost). Bear in mind that this may return NULL or throw on failure.

                我在回想我实际使用过reinterpret_cast的次数,实际上只有两个:

                I'm trying to think of times when I've actually used reinterpret_cast, there are really only two:

                • 当一个函数正在压缩/加密任意缓冲区时,我想使用 const char * 来遍历它
                • if(*reinterpret_cast<uint32_t*>(array_of_4_bytes_A) < *reinterpret_cast<uint32_t*>(array_of_4_bytes_B) 或诸如此类.像这样的行会引起审查和评论.
                • when a function is zipping/encrypting an arbitrary buffer and I want to use a const char * to traverse it
                • if(*reinterpret_cast<uint32_t*>(array_of_4_bytes_A) < *reinterpret_cast<uint32_t*>(array_of_4_bytes_B) or somesuch. Lines like this invite scrutiny and demand comments.

                否则,如果您有一个 A* 实际上是一个 B*,那么您可能需要一个联合.

                Otherwise if you have a A* which is really a B* then you probably want a union.

                这篇关于C++ 什么时候我们应该更喜欢使用两个链接的 static_cast 而不是 reinterpret_cast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:我应该在 C 样式转换上使用 C++ reinterpret_cast 吗? 下一篇:C++ 不能通过虚基 A 从基 A 转换为派生类型 B

                相关文章

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

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

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

                  <legend id='wI8wH'><style id='wI8wH'><dir id='wI8wH'><q id='wI8wH'></q></dir></style></legend>