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

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

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

        在两个相同类的指针之间进行转换的安全性?

        时间:2023-09-26

            • <tfoot id='wAwtj'></tfoot>

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

                  <bdo id='wAwtj'></bdo><ul id='wAwtj'></ul>
                  <i id='wAwtj'><tr id='wAwtj'><dt id='wAwtj'><q id='wAwtj'><span id='wAwtj'><b id='wAwtj'><form id='wAwtj'><ins id='wAwtj'></ins><ul id='wAwtj'></ul><sub id='wAwtj'></sub></form><legend id='wAwtj'></legend><bdo id='wAwtj'><pre id='wAwtj'><center id='wAwtj'></center></pre></bdo></b><th id='wAwtj'></th></span></q></dt></tr></i><div id='wAwtj'><tfoot id='wAwtj'></tfoot><dl id='wAwtj'><fieldset id='wAwtj'></fieldset></dl></div>
                  <legend id='wAwtj'><style id='wAwtj'><dir id='wAwtj'><q id='wAwtj'></q></dir></style></legend>
                    <tbody id='wAwtj'></tbody>
                  本文介绍了在两个相同类的指针之间进行转换的安全性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有两个不同的类,它们都以相同的内部方式表示二维坐标数据,如下所示:

                  Let's say I have two different classes, both represent 2D coordinate data in the same internal way like the following:

                  class LibA_Vertex{
                      public:
                      // ... constructors and various methods, operator overloads
                      float x, y
                  };
                  
                  class LibB_Vertex{
                      public:
                      // ... same usage and internal data as LibA, but with different methods
                      float x, y
                  };
                  
                  
                  void foobar(){
                      LibA_Vertex * verticesA = new LibA_Vertex[1000];
                      verticesA[50].y = 9;
                      LibB_Vertex * verticesB = reinterpret_cast<LibB_Vertex*>( vertexA );
                      print(verticesB[50].y); // should output a "9"
                  };
                  

                  鉴于这两个类和上面的函数是相同的,我能否可靠地指望这种指针转换在每种情况下都能按预期工作?

                  Given the two classes being identical and the function above, can I reliably count on this pointer conversion working as expected in every case?

                  (背景是,我需要一种简单的方法在两个具有相同 Vertex 类的独立库之间交换顶点数组,并且我想避免不必要地复制数组).

                  (The background, is that I need an easy way of trading vertex arrays between two separate libraries that have identical Vertex classes, and I want to avoid needlessly copying arrays).

                  推荐答案

                  C++11 添加了一个名为 layout-compatible 的概念,适用于此处.

                  C++11 added a concept called layout-compatible which applies here.

                  如果两个标准布局结构(第 9 条)类型具有相同数量的非静态数据成员和相应的非静态数据成员,则它们是布局兼容(按声明顺序)具有布局兼容类型(3.9).

                  Two standard-layout struct (Clause 9) types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types (3.9).

                  哪里

                  标准布局类是这样一个类:

                  A standard-layout class is a class that:

                  • 没有非标准布局类(或此类类型的数组)或引用类型的非静态数据成员,
                  • 没有虚函数 (10.3) 和虚基类 (10.1),
                  • 对所有非静态数据成员具有相同的访问控制(第 11 条),
                  • 没有非标准布局的基类,
                  • 要么在最派生的类中没有非静态数据成员且至多有一个具有非静态数据成员的基类,要么没有具有非静态数据成员的基类,并且
                  • 没有与第一个非静态数据成员相同类型的基类.

                  standard-layout struct 是使用 class-key struct 定义的 standard-layout classclass-key class.

                  A standard-layout struct is a standard-layout class defined with the class-key struct or the class-key class.

                  standard-layout union 是使用 class-key union 定义的 standard-layout 类.

                  A standard-layout union is a standard-layout class defined with the class-key union.

                  终于

                  指向 cv-qualified 和 cv-unqualified 版本 (3.9.3) 的布局兼容的指针类型应具有相同的值表示和对齐要求(3.11).

                  Pointers to cv-qualified and cv-unqualified versions (3.9.3) of layout-compatible types shall have the same value representation and alignment requirements (3.11).

                  这保证了 reinterpret_cast 可以将指向一种类型的指针转换为指向任何布局兼容类型的指针.

                  Which guarantees that reinterpret_cast can turn a pointer to one type into a pointer to any layout-compatible type.

                  这篇关于在两个相同类的指针之间进行转换的安全性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++.错误:void 不是指向对象的类型 下一篇:我应该在 C 样式转换上使用 C++ reinterpret_cast 吗?

                  相关文章

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

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

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

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