Serialization: Making a Serializable Class ----------------------------------------------- Five main steps are required to make a class serializable. They are listed below and explained in the following sections: 1. Deriving your class from CObject (or from some class derived from CObject). 2. Overriding the Serialize member function. 3 .Using the DECLARE_SERIAL macro in the class declaration. 4. Defining a constructor that takes no arguments. 5. Using the IMPLEMENT_SERIAL macro in the implementation file for your class. If you call Serialize directly rather than through the >> and << operators of CArchive, the last three steps are not required for serialization.
1. 整个软件我还没完成, 还不能运行到这块, 目前只是在写类拉口, 2. 如果是个类我觉得一切都有可能,关键是POINT, RECT,是一个地地道道的C结构 typedef struct tagPOINT { LONG x; LONG y; } POINT;typedef struct tagRECT { LONG left; LONG top; LONG right; LONG bottom; } RECT;
-----------------------------------------------
Five main steps are required to make a class serializable. They are listed below and explained in the following sections: 1. Deriving your class from CObject (or from some class derived from CObject). 2. Overriding the Serialize member function. 3 .Using the DECLARE_SERIAL macro in the class declaration. 4. Defining a constructor that takes no arguments. 5. Using the IMPLEMENT_SERIAL macro in the implementation file for your class. If you call Serialize directly rather than through the >> and << operators of CArchive, the last three steps are not required for serialization.
RECT rect1;
MYSTRUCT struct1; if(ar.IsStoring())
{
ar<<dot1; //这个结构体是直接支持的
ar<<rect1; //这个结构体是直接支持的
ar<<struct1; //自定义结构体不技持会报错, 只能写下如下:
ar.Write(&struct1, sizeof(struct1));
}
else
{
//读
}
例如CRect进入下面函数了。
_AFXWIN_INLINE CArchive& AFXAPI operator<<(CArchive& ar, const RECT& rect)
{ ar.Write(&rect, sizeof(RECT)); return ar; }
2. 如果是个类我觉得一切都有可能,关键是POINT, RECT,是一个地地道道的C结构
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT;typedef struct tagRECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT;
_AFXWIN_INLINE CArchive& AFXAPI operator<<(CArchive& ar, SIZE size)
{ ar.Write(&size, sizeof(SIZE)); return ar; }
_AFXWIN_INLINE CArchive& AFXAPI operator<<(CArchive& ar, POINT point)
{ ar.Write(&point, sizeof(POINT)); return ar; }
_AFXWIN_INLINE CArchive& AFXAPI operator<<(CArchive& ar, const RECT& rect)
{ ar.Write(&rect, sizeof(RECT)); return ar; }
_AFXWIN_INLINE CArchive& AFXAPI operator>>(CArchive& ar, SIZE& size)
{ ar.Read(&size, sizeof(SIZE)); return ar; }
_AFXWIN_INLINE CArchive& AFXAPI operator>>(CArchive& ar, POINT& point)
{ ar.Read(&point, sizeof(POINT)); return ar; }
_AFXWIN_INLINE CArchive& AFXAPI operator>>(CArchive& ar, RECT& rect)
{ ar.Read(&rect, sizeof(RECT)); return ar; }
我上面给你的函数明明是一个地地道道的C结构,为什么你觉得类可能地地道道的C结构就不可能?你仔细看我帖子了?