Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

CArchive::MapObject 

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 
);

Parameters

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;
    ...
   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 << (CObject *)m_pDoc;
      ar << m_l;
   }
   else
   {
      // Will load a reference to
      // the "mapped" document pointer
      ar >> (CObject *&) m_pDoc;
      ar >> m_l;
   }
}

Community Additions

ADD
Show:
© 2015 Microsoft