CArchive::MapObject
调用该成员函数将实际上不会序列化为文件,但是,请供子对象可以引用的对象在映射。
void MapObject(
const CObject* pOb
);
参数
- pOb
用于存储对象的常数的指针。
备注
例如,您可能无法序列化文档,但是,您会序列化是文档的一部分的项目。 通过调用 MapObject,您可以在项目或子对象,引用文档。 此外,序列化的子项可以序列化其 m_pDocument 返回一个指针。
在存储到并从 CArchive 对象时,加载可以调用 MapObject。 MapObject 添加到 CArchive 对象维护的内部数据结构的指定对象在序列化和反序列化时,但是,不同 ReadObject 和 WriteObject, 它在对象不调用序列化。
示例
//MyDocument.h
class CMyDocument : public CDocument
{
public:
DECLARE_SERIAL(CMyDocument)
CObList m_listOfSubItems;
virtual void Serialize(CArchive& ar);
};
//MyDocument.cpp
IMPLEMENT_SERIAL(CMyDocument, CDocument, 1)
void CMyDocument::Serialize(CArchive& ar)
{
CDocument::Serialize(ar);
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
ar.MapObject(this);
//serialize the subitems in the document;
//they will be able to serialize their m_pDoc
//back pointer
m_listOfSubItems.Serialize(ar);
}
//SubItem.h
class CSubItem : public CObject
{
DECLARE_SERIAL(CSubItem)
CSubItem() : m_i(0) {};
public:
CSubItem(CMyDocument * pDoc)
{ m_pDoc = pDoc; }
// back pointer to owning document
CMyDocument* m_pDoc;
WORD m_i; // other item data
virtual void Serialize(CArchive& ar);
};
//SubItem.cpp
IMPLEMENT_SERIAL(CSubItem, CObject, 1);
void CSubItem::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// will serialize a reference
// to the "mapped" document pointer
ar << (CObject *)m_pDoc;
ar << m_i;
}
else
{
// Will load a reference to
// the "mapped" document pointer
ar >> (CObject *&) m_pDoc;
ar >> m_i;
}
}
要求
Header: afx.h