c++ - RECT 和 POINT 数组之间的 reinterpret_cast 安全吗?

标签 c++ windows winapi visual-c++ casting

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/

相关文章:

windows - 如何识别Windows dir命令中的日期格式?

windows - 使用 Register-WmiEvent 使用 PowerShhell 4.0 捕获 Windows 8.1 系统事件/触发器?

python - 如何确定 Windows、Linux 和 MacOS 中 Python 的显示空闲时间?

c++ - 使用派生类的程序的错误输出。不为心之光

c++ - 有没有办法在 C++ 中将字节数组转换为数字?

c++ - Opencv 在 Qt5.13 中不工作。应用程序总是在 Windows 中崩溃

c++ - 将 win32_logicaldisk 映射到 win32_diskdrive?

c++ - 仅编译已更改文件的共享库的 Makefile?

将操作系统线程限制在一个 CPU 上会很糟糕吗?

c++ - gdi+ GetWorldTransform() 它在 directx 中的什么位置?