试图坚持使用MFC框架,处理指向serialize()
中字节的指针的正确方法是什么?例如,假设您有:
PBYTE m_TheData;
SIZE_T m_TheDataSize;
TIA !!
试图坚持使用MFC框架,处理指向serialize()
中字节的指针的正确方法是什么?例如,假设您有:
PBYTE m_TheData;
SIZE_T m_TheDataSize;
TIA !!
在Serialize()
的类重写中,您可以使用以下代码读取/写入m_TheData
字节数组成员-及其大小-
void MyClass::Serialize(CArchive &ar)
{
CDocument::Serialize(ar); // Replace "CDocument" with your IMMEDIATE base class!
// Note: the "SIZE_T" type varies between 32- and 64-bit platforms/builds ...
uint64_t dsFixed; // ... use this to guarantee a 64-bit 'size' write/read!
if (ar.IsStoring()) {
//... Write other stuff...
// ...making sure you keep STRICT order compliance between write and read
dsFixed = uint64_t(m_TheDataSize);
ar << dsFixed; // Write the array size first...
ar.Write(m_TheData,INT(m_TheDataSize)); // ...then the data
//...
}
else {
//... Read other stuff (see note above)
// delete m_TheData; // Do this if it's not a newly-created class?
ar >> dsFixed; // Read the array size first...
m_TheDataSize = SIZE_T(dsFixed);
m_TheData = new BYTE[m_TheDataSize]; // Allocate space for the data...
ar.Read(m_TheData,UINT(m_TheDataSize)); // ...then load data from archive
//...
}
return;
}
随时要求进一步的澄清和/或解释。
关于SIZE_T
类型的注释:
尽管使用的方式与标准size_t
类型几乎相同,但特定于MSVC
的{{1}}是在SIZE_T
中定义的,由basetsd.h
或{ {1}}如下:Windows.h
,其中afxwin.h
本身定义为typedef ULONG_PTR SIZE_T;
(64位版本)或ULONG_PTR
(32位版本)。 / p>