#pragma pack() 引發的問題
阿新 • • 發佈:2019-02-04
記錄下最近遇到的問題,寫了一個類,宣告如下:
class DeletedFile { public: virtual size_t Read(char *pBuf, size_t nBufSize) = 0; virtual INT32 Seek(INT32 nOffLow, INT32 *nOffHigh, UINT32 nMoveMethod); //參考了SetFileFilter的定義 DeletedFile(const char *pFileName, UINT64 nInFileIndex, UINT64 nInFileSize); UINT64 GetFileSize(); protected: UINT64 nFileIndex; //刪除檔案的簇號 UINT64 nFileSize; //刪除檔案的大小 UINT64 nCurPosition; //當前讀取到的位置(在檔案中) };
奇葩的問題來了,後面在建構函式中對成員賦值一直出錯,如下:
最終才發現是另一個頭檔案中的#pragma pack()未關閉引發的問題,stackflow上也有關於這種問題的解釋:
引用其中的話
“Without this, having the packing done via a separate header will lead to confusions, when it's added in one TU while not in another TU.”