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