Windows SDK 包含一组 typedef:
typedef long LONG;
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT;
typedef struct tagRECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT;
然后,有一个 WinAPI 函数需要一个指向 POINT
结构数组的指针和该数组的长度:
void ThatFunction( POINT* points, int numberOfElements );
我们有以下代码:
RECT rect = ...//obtained from somewhere
ThatFunction( reinterpret_cast<POINT*>( &rect ), 2 );
因此 RECT
被视为两个 POINT
结构的数组。
这样的转换安全吗?
最佳答案
因为 Windows 开发人员在 WinDef.h 中使用相同的包装在每个旁边声明了 RECT 和 POINT,所以您几乎可以假设它是安全的。 Win32 API,MapWindowPoints , 是可以传递 RECT 或一对 POINT 的函数示例。文档甚至建议这样使用它。
关于c++ - RECT 和 POINT 数组之间的 reinterpret_cast 安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4848905/