Export (0) Print
Expand All


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.

void MapObject(
   const CObject* pOb 


A constant pointer to the object being stored.


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.


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

class CMyDocument : public CDocument
   CObList m_listOfSubItems;

// MyDoc.cpp
IMPLEMENT_SERIAL(CMyDocument, CObject, 1)
void CMyDocument::Serialize(CArchive& ar)
   if (ar.IsStoring())
      // TODO: add storing code here
      // TODO: add loading code here

   //serialize the subitems in the document;
   //they will be able to serialize their m_pDoc
   //back pointer


class CSubItem : public CObject
   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);

void CSubItem::Serialize(CArchive& ar)

   if (ar.IsStoring( ) )
      // will serialize a reference 
      // to the "mapped" document pointer
      ar << (CObject *)m_pDoc;
      ar << m_l;
      // Will load a reference to
      // the "mapped" document pointer
      ar >> (CObject *&) m_pDoc;
      ar >> m_l;

See Also

CArchive Overview | Class Members | Hierarchy Chart | CArchive::ReadObject | CArchive::WriteObject

© 2014 Microsoft