This documentation is archived and is not being maintained.

Customizing an Object Dump

When you derive a class from CObject, you can override the Dump member function to provide additional information when you use DumpAllObjectsSince to dump objects to the Output window.

The Dump function writes a textual representation of the object's member variables to a dump context (CDumpContext). The dump context is similar to an I/O stream. You can use the insertion (<<) operator to send data to a CDumpContext.

When you override the Dump function, you should first call the base class version of Dump to dump the contents of the base class object. Then output a textual description and value for each member variable of your derived class.

The declaration of the Dump function looks like this:

class CPerson : public CObject
#ifdef _DEBUG
   virtual void Dump( CDumpContext& dc ) const;

   CString m_firstName;
   CString m_lastName;
   // And so on...

Because object dumping only makes sense when you are debugging your program, the declaration of the Dump function is bracketed with an #ifdef _DEBUG / #endif block.

In the following example, the Dump function first calls the Dump function for its base class. It then writes a short description of each member variable along with the member's value to the diagnostic stream.

#ifdef _DEBUG
void CPerson::Dump( CDumpContext& dc ) const
   // Call the base class function first.
   CObject::Dump( dc );

   // Now do the stuff for our specific class.
   dc << "last name: " << m_lastName << "\n"
      << "first name: " << m_firstName << "\n";

You must supply a CDumpContext argument to specify where the dump output will go. The Debug version of MFC supplies a predefined CDumpContext object named afxDump that sends output to the debugger.

CPerson* pMyPerson = new CPerson;
// Set some fields of the CPerson object.
// Now dump the contents.
#ifdef _DEBUG
pMyPerson->Dump( afxDump );

See Also

Object Dumps | The TRACE Macro