<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

      C++ When should we prefer to use a two chained static_cast over reinterpret_cast(C++ 什么时候我们应该更喜欢使用两个链接的 static_cast 而不是 reinterpret_cast)

      • <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                <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'>

                      <tfoot id='wI8wH'></tfoot>
                        <tbody id='wI8wH'></tbody>

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