CArchive::MapObject

void MapObject( const CObject* pOb );

Parameters

pOb

A constant pointer to the object being stored.

Remarks

Call this member function to place objects in the map that are not really serialized to the file, but that are available for subobjects to reference. For example, you might not serialize a document, but you would serialize the items that are part of the document.  By calling MapObject, you allow those items, or subobjects, to reference the document. Also, serialized subitems can serialize their m_pDocument back pointer.

You can call MapObject when you store to and load from the CArchive object. MapObject adds the specified object to the internal data structures maintained by the CArchive object during serialization and deserialization, but unlike ReadObject and WriteObject, it does not call serialize on the object.

Example

// MyDoc.h
// Document should have DECLARE_SERIAL and IMPLEMENT_SERIAL

class CMyDocument : public CDocument
{
   CObList m_listOfSubItems;
    ...
   DECLARE_SERIAL(CMyDocument)
};

// MyDoc.cpp
...
IMPLEMENT_SERIAL(CMyDocument, CObject, 1)
...
void CMyDocument::Serialize(CArchive& 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
{
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
void CSubItem::Serialize(CArchive& ar)
{
   if (ar.IsStoring())
   {
      // will serialize a reference 
      //to the "mapped" document pointer
      ar << m_pDoc;  
      ar << m_i;
   }
   else
   {
      // will load a reference to 
      //the "mapped" document pointer
      ar >> m_pDoc;
      ar >> m_i;
   }
}

CArchive OverviewClass MembersHierarchy Chart

See Also   CArchive::ReadObject, CArchive::WriteObject