两个相同类指针之间的转换安全性? [英] Safety of casting between pointers of two identical classes?

查看:148
本文介绍了两个相同类指针之间的转换安全性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  LibA_Vertex {
public:
// ...构造函数和各种方法,运算符重载
float x,y
};

class LibB_Vertex {
public:
// ...与LibA相同的用法和内部数据,但使用不同的方法
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 a9
};

给定两个类相同和上面的函数,我可以可靠地依赖这个指针转换工作预期在每种情况下?



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

解决方案

C ++ 11添加了一个名为布局兼容 >


两个标准布局结构(第9条)类型是布局兼容相同数量的非静态数据成员和对应的非静态数据成员(以声明顺序)具有布局兼容类型(3.9)。


其中


标准布局类是一个类:




  • 没有类型为非标准布局类(或此类类型的数组)或引用的非静态数据成员,

  • 没有虚拟函数(10.3)和没有虚拟基类(10.1),

  • 对所有非静态数据成员具有相同的访问控制(第11条),

  • 没有非标准布局基类,

  • 在大多数派生类中没有非静态数据成员,最多一个基类具有非静态数据成员或没有具有非静态数据成员的基类,且

  • 没有与第一个非静态数据成员相同类型的基类。



一个 standard-layout struct 是一个标准布局类键 c c c / c>

是一个标准布局类,用 class-key code> union 。


最后


布局兼容的
类型的cv限定和cv非限定版本(3.9.3)的指针应具有相同的值表示和对齐要求(3.11)。


这保证 reinterpret_cast 可以将指针指向一种类型, - 兼容类型。


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?

(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 added a concept called layout-compatible which applies here.

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).

where

A standard-layout class is a class that:

  • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • has no virtual functions (10.3) and no virtual base classes (10.1),
  • has the same access control (Clause 11) for all non-static data members,
  • has no non-standard-layout base classes,
  • either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes with non-static data members, and
  • has no base classes of the same type as the first non-static data member.

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

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

Finally

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).

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆