求一C或C++编写的程序,实现串行化功能

2024-12-27 11:01:25
推荐回答(3个)
回答1:

串行化主要有五个步骤,你可以参考文档写啊,其实不难的。
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.
例子:
class CPerson : public CObject
{
public:
DECLARE_SERIAL( CPerson )
// empty constructor is necessary
CPerson(){};

CString m_name;
WORD m_number;

void Serialize( CArchive& archive );

// rest of class declaration
};

To override the Serialize member function

Call your base class version of Serialize to make sure that the inherited portion of the object is serialized.

Insert or extract the member variables specific to your class.
The insertion and extraction operators interact with the archive class to read and write the data. The following example shows how to implement Serialize for the CPerson class declared above:

void CPerson::Serialize( CArchive& archive )
{
// call base class function first
// base class is CObject in this case
CObject::Serialize( archive );

// now do the stuff for our specific class
if( archive.IsStoring() )
archive << m_name << m_number;
else
archive >> m_name >> m_number;
}

回答2:

不明所以,你描述的东西扯都扯不到一起。先研究研究要你的课题,你就知道怎么做了,至少知道该怎么提问。

回答3:

这个我也学习学习。。