SERIALIZING A DOCUMENT

This all sounds rather tricky, but the basic capability for serializing your document was built into the application by the Application Wizard right at the outset. The handlers for the File ⇒ Save, File ⇒ Save As, and File ⇒ Open menu items all assume that you want serialization implemented for your document, and already contain the code to support it. Take a look at the parts of the definition and implementation of CSketcherDoc that relate to creating a document using serialization.

Serialization in the Document Class Definition

The code in the definition of CSketcherDoc that enables serialization of a document object is shown shaded in the following fragment:

class CSketcherDoc : public CDocument
{
protected: // create from serialization only
   CSketcherDoc();
   DECLARE_DYNCREATE(CSketcherDoc)
        
// Rest of the class...
        
// Overrides
   public:
   virtual BOOL OnNewDocument();
   virtual void Serialize(CArchive& ar);
        
// Rest of the class...
        
};

There are three things here that relate to serializing a document object:

1. The DECLARE_DYNCREATE() macro.
2. The Serialize() member function.
3. The default class constructor.

DECLARE_DYNCREATE() is a macro that enables objects of the CSketcherDoc class to be created dynamically by the application framework during the serialization input process. It’s matched by a complementary macro, IMPLEMENT_DYNCREATE(), in the class implementation. These macros apply only to classes derived from CObject, but as you will see shortly, they ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.