使用存档来存储和加载CObject
需要额外的考虑。 在某些情况下,应调用 Serialize
对象的函数,其中 CArchive
对象是调用的参数 Serialize
,而不是使用 << 或 >> 运算符 CArchive
。 需要记住的一个要点是,CArchive
>> 运算符基于先前通过存储存档写入文件的 CObject
信息在内存中构造 CRuntimeClass
。
因此,是否使用 CArchive
<< 和 >> 运算符(而不是调用 Serialize
) 取决于是否需要加载 存档根据以前存储 CRuntimeClass
的信息动态重新构造对象。 在以下情况下使用 Serialize
函数:
反序列化对象时,事先知道对象的确切类。
反序列化对象时,已事先为其分配内存。
谨慎
如果使用函数加载对象 Serialize
,则还必须使用该 Serialize
函数存储对象。 不要使用 CArchive
<< 运算符进行存储,然后使用 Serialize
函数进行加载,或使用 Serialize
函数进行存储,然后使用 CArchive >>
运算符进行加载。
以下示例演示了这些情况:
class CMyObject : public CObject
{
// ...Member functions
public:
CMyObject() {}
virtual void Serialize(CArchive &ar);
// Implementation
protected:
DECLARE_SERIAL(CMyObject)
};
class COtherObject : public CObject
{
// ...Member functions
public:
COtherObject() {}
virtual void Serialize(CArchive &ar);
// Implementation
protected:
DECLARE_SERIAL(COtherObject)
};
class CCompoundObject : public CObject
{
// ...Member functions
public:
CCompoundObject();
~CCompoundObject();
virtual void Serialize(CArchive &ar);
// Implementation
protected:
CMyObject m_myob; // Embedded object
COtherObject *m_pOther; // Object allocated in constructor
CObject *m_pObDyn; // Dynamically allocated object
//..Other member data and implementation
DECLARE_SERIAL(CCompoundObject)
};
IMPLEMENT_SERIAL(CMyObject, CObject, 1)
IMPLEMENT_SERIAL(COtherObject, CObject, 1)
IMPLEMENT_SERIAL(CCompoundObject, CObject, 1)
CCompoundObject::CCompoundObject()
{
m_pOther = new COtherObject; // Exact type known and object already
//allocated.
m_pObDyn = NULL; // Will be allocated in another member function
// if needed, could be a derived class object.
}
CCompoundObject::~CCompoundObject()
{
delete m_pOther;
}
void CCompoundObject::Serialize(CArchive &ar)
{
CObject::Serialize(ar); // Always call base class Serialize.
m_myob.Serialize(ar); // Call Serialize on embedded member.
m_pOther->Serialize(ar); // Call Serialize on objects of known exact type.
// Serialize dynamic members and other raw data
if (ar.IsStoring())
{
ar << m_pObDyn;
// Store other members
}
else
{
ar >> m_pObDyn; // Polymorphic reconstruction of persistent object
//load other members
}
}
总之,如果您的可序列化类将嵌入 CObject
定义为成员,则不应使用该 CObject
对象的 CArchive
, <<, 和 >> 运算符,而是应该调用 Serialize
函数。 此外,如果可序列化类将指向 CObject
某个(或派生自 CObject
的对象)的指针定义为成员,但在其自己的构造函数中构造此其他对象,则还应调用 Serialize
。