1.除了要包含必须的头文件,关键是要选择正确的MFC运行库链接方式:Project->Setting->General->"Use MFC in a Shared DLL"。
2.这是一个自定义对象两种持久化方式比较的测试例子:
#include <afxwin.h> // MFC core and standard components//#include <afxext.h>// MFC extensions
//#include <afxdisp.h> // MFC Automation classes
//#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
//#include <afxcmn.h> // MFC support for Windows Common Controls
class CMyClass : public CObject
{
DECLARE_SERIAL(CMyClass)
public:
CMyClass(){}
CMyClass(LPCTSTR lpszClassName){m_cString=lpszClassName}
~CMyClass(){}
void Dump(CDumpContext&dc) const
void Serialize(CArchive&ar)
CString m_cString
}
IMPLEMENT_SERIAL(CMyClass,CObject,0)
void CMyClass::Dump(CDumpContext&dc) const
{
if(m_cString.GetLength() >0)
{
dc <<(LPTSTR)(LPCTSTR)m_cString
dc <<_T("\r\n")
}
}
void CMyClass::Serialize(CArchive&ar)
{
TCHAR* ptc=(LPTSTR)(LPCTSTR)m_cString
for(int i=0i<m_cString.GetLength()i++)
{
ar <<ptc[i]
}
ar <<'\r'
ar <<'\n'
}
int main()
{
CMyClass* pobj = new CMyClass(_T("请把我持久化,谢谢!"))
CFile cfile1(_T("dump1.txt"),CFile::modeReadWrite|CFile::modeCreate)
CDumpContext cdc(&cfile1)
pobj->Dump(cdc)
CFile cfile2(_T("dump2.txt"),CFile::modeReadWrite|CFile::modeCreate)
CArchive ar(&cfile2,0)
pobj->Serialize(ar)
Sleep(2000)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)