Windows SDK中包含了一组类型定义的:RECT和POINT数组之间的reinterpret_cast是否安全?
typedef long LONG;
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT;
typedef struct tagRECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT;
然后,有一个期望一个指向POINT
结构数组,该数组的长度WinAPI的函数:
void ThatFunction(POINT* points, int numberOfElements);
,我们有以下代码:
RECT rect = ...//obtained from somewhere
ThatFunction(reinterpret_cast<POINT*>(&rect), 2);
使得RECT
被视为两个阵列结构。
就是这样铸造安全吗?
+1对于C++ 0x static_assert()的使用很好的例子。 – 2011-01-31 13:37:08