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

      <tfoot id='pSIYY'></tfoot>
      <legend id='pSIYY'><style id='pSIYY'><dir id='pSIYY'><q id='pSIYY'></q></dir></style></legend>

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

      2. 多重继承:从 void * 转换到第二个基类后的意外结果

        时间:2023-09-26
          <bdo id='yq9GP'></bdo><ul id='yq9GP'></ul>

              <tbody id='yq9GP'></tbody>

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

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

                1. <legend id='yq9GP'><style id='yq9GP'><dir id='yq9GP'><q id='yq9GP'></q></dir></style></legend>
                  本文介绍了多重继承:从 void * 转换到第二个基类后的意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的程序需要使用void*来在动态调用的情况下传输数据或对象,以便它可以引用任意类型的数据,甚至是原始类型的数据.但是,我最近发现在具有多个基类的类的情况下向下转换这些 void* 的过程失败,甚至在调用这些向下转换的指针上的方法后使我的程序崩溃,即使内存地址似乎是正确的.崩溃发生在访问vtable"期间.

                  My program needs to make use of void* in order to transport data or objects in dynamic invocation situation, so that it can reference data of arbitrary types, even primitive types. However, I recently discovered that the process of down-casting these void* in case of classes with multiple base classes fails and even crashes my program after invoking methods on these down casted pointers even if the memory addresses seem to be correct. The crash happens during access to "vtable".

                  所以我创建了一个小测试用例,环境是Mac OS X上的gcc 4.2:

                  So I have created a small test case, environment is gcc 4.2 on Mac OS X:

                  class Shape {
                  public:
                      virtual int w() = 0;
                      virtual int h() = 0;
                  };
                  
                  class Square : public Shape {
                  public:
                      int l;
                      int w() {return l;}
                      int h() {return l;}
                  };
                  
                  class Decorated {
                  public:
                      int padding;
                      int w() {return 2*padding;}
                      int h() {return 2*padding;}
                  };
                  
                  class DecoratedSquare : public Square, public Decorated {
                  public:
                      int w() {return Square::w() + Decorated::w();}
                      int h() {return Square::h() + Decorated::h();}
                  };
                  
                  
                  #include <iostream>
                  
                  template <class T> T shape_cast(void *vp) {
                  //    return dynamic_cast<T>(vp);   // not possible, no pointer to class type
                  //    return static_cast<T>(vp);
                  //    return T(vp);
                  //    return (T)vp;
                      return reinterpret_cast<T>(vp);
                  }
                  
                  int main(int argc, char *argv[]) {
                      DecoratedSquare *ds = new DecoratedSquare;
                      ds->l = 20;
                      ds->padding = 5;
                      void *dsvp = ds;
                  
                      std::cout << "Decorated (direct)" << ds->w() << "," << ds->h() << std::endl;
                  
                      std::cout << "Shape " << shape_cast<Shape*>(dsvp)->w() << "," << shape_cast<Shape*>(dsvp)->h() << std::endl;
                      std::cout << "Square " << shape_cast<Square*>(dsvp)->w() << "," << shape_cast<Square*>(dsvp)->h() << std::endl;
                      std::cout << "Decorated (per void*) " << shape_cast<Decorated*>(dsvp)->w() << "," << shape_cast<Decorated*>(dsvp)->h() << std::endl;
                      std::cout << "DecoratedSquare " << shape_cast<DecoratedSquare*>(dsvp)->w() << "," << shape_cast<DecoratedSquare*>(dsvp)->h() << std::endl;
                  }
                  

                  产生以下输出:

                  Decorated (direct)30,30
                  Shape 30,30
                  Square 30,30
                  Decorated (per void*) 73952,73952
                  DecoratedSquare 30,30
                  

                  如您所见,装饰(每个空隙*)"结果是完全错误的.它也应该像第一行一样是 30,30.

                  As you can see, the "Decorated (per void*)" result is completely wrong. It should also be 30,30 like in the first line.

                  无论我在 shape_cast() 中使用什么强制转换方法,我都会为装饰部分获得相同的意外结果.这些 void * 完全有问题.

                  Whatever cast method I use in shape_cast() I will always get the same unexpected results for the Decorated part. Something is completely wrong with these void *.

                  根据我对 C++ 的理解,这应该是可行的.有没有机会让它与空*一起工作?这可能是 gcc 中的错误吗?

                  From my understanding of C++ this should be actually working. Is there any chance to get this to work with the void*? Can this be a bug in gcc?

                  谢谢

                  推荐答案

                  这不是编译器错误 - 这是 reinterpret_cast 所做的.DecoratedSquare 对象将在内存中布局如下:

                  It's not a compiler bug - it's what reinterpret_cast does. The DecoratedSquare object will be laid out in memory something like this:

                  Square
                  Decorated
                  DecoratedSquare specific stuff
                  

                  将指向 this 的指针转换为 void* 将给出此数据的起始地址,而不知道那里是什么类型.reinterpret_cast 将获取该地址并将其中的任何内容解释为 Decorated - 但实际的内存内容是 Square.这是错误的,所以你会得到未定义的行为.

                  Converting a pointer to this to void* will give the address of the start of this data, with no knowledge of what type is there. reinterpret_cast<Decorated*> will take that address and interpret whatever is there as a Decorated - but the actual memory contents are the Square. This is wrong, so you get undefined behaviour.

                  如果你reinterpret_cast 到正确的动态类型(即DecoratedSquare),你应该得到正确的结果,然后转换到基类.

                  You should get the correct results if you reinterpret_cast to the correct dynamic type (that is DecoratedSquare), then convert to the base class.

                  这篇关于多重继承:从 void * 转换到第二个基类后的意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:可以对内置类型使用 C 风格的强制转换吗? 下一篇:“.f"的目的附加到一个数字?

                  相关文章

                  <tfoot id='rl6cv'></tfoot>

                2. <small id='rl6cv'></small><noframes id='rl6cv'>

                    <bdo id='rl6cv'></bdo><ul id='rl6cv'></ul>

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

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