在D:\Program Files\Microsoft Visual Studio\VC98\Include\WINUSER.H 中有这样一段代码:
#define MAKEINTRESOURCEA(i) (LPSTR)((DWORD)((WORD)(i)))
#define MAKEINTRESOURCEW(i) (LPWSTR)((DWORD)((WORD)(i)))
#ifdef UNICODE
#define MAKEINTRESOURCE MAKEINTRESOURCEW
#else
#define MAKEINTRESOURCE MAKEINTRESOURCEA
#endif // !UNICODE请问各位大虾强制类型内部是如何实现的?为啥上面的
(LPSTR)((DWORD)((WORD)(i)))
不可以简化成
(LPSTR)(i)呢?
#define MAKEINTRESOURCEA(i) (LPSTR)((DWORD)((WORD)(i)))
#define MAKEINTRESOURCEW(i) (LPWSTR)((DWORD)((WORD)(i)))
#ifdef UNICODE
#define MAKEINTRESOURCE MAKEINTRESOURCEW
#else
#define MAKEINTRESOURCE MAKEINTRESOURCEA
#endif // !UNICODE请问各位大虾强制类型内部是如何实现的?为啥上面的
(LPSTR)((DWORD)((WORD)(i)))
不可以简化成
(LPSTR)(i)呢?
(WORD)(i)确保了这一点 so as to trim unwanted high bytes.
then force convert to pointer, because a pointer is expressed by 4 bytes on x86
so we have to convert again the WORD to DWORD(4 bytes), finally, convert into LPSTR or LPWSTR for successfully compiling.
LPSTR pStr1 = (LPSTR)((DWORD)((WORD)(56)));
LPSTR pStr2 = (LPSTR)((DWORD)(56));
LPSTR pStr3 = (LPSTR)(56);
CString str;
str.Format("pStr1 = %p, pStr2 = %p, pStr3 = %p", pStr1, pStr2, pStr3);
AfxMessageBox(str);结果是:pStr1 = 00000038, pStr2 = 00000038, pStr3 = 00000038