Partager via


CArchive::SerializeClass

Call this member function when you want to store and load the version information of a base class.

void SerializeClass(
   const CRuntimeClass* pClassRef 
);

Parameters

  • pClassRef
    A pointer to a run-time class object for the base class.

Remarks

SerializeClass reads or writes the reference to a class to the CArchive object, depending on the direction of the CArchive. Use SerializeClass in place of ReadClass and WriteClass as a convenient way to serialize base-class objects; SerializeClass requires less code and fewer parameters.

Like ReadClass, SerializeClass verifies that the archived class information is compatible with your runtime class. If it is not compatible, SerializeClass will throw a CArchiveException.

Your runtime class must use DECLARE_SERIAL and IMPLEMENT_SERIAL; otherwise, SerializeClass will throw a CNotSupportedException.

Use the RUNTIME_CLASS macro to retrieve the value for the pRuntimeClass parameter. The base class must have used the IMPLEMENT_SERIAL macro.

Example

class CBaseClass : public CObject 
{
   DECLARE_SERIAL(CBaseClass);
};
class CDerivedClass : public CBaseClass
{
public:
   virtual void Serialize(CArchive& ar);
};
void CDerivedClass::Serialize(CArchive& ar)
{
   if (ar.IsStoring())
   {
      //normal code for storing contents 
      //of this object
   }
   else
   {
      //normal code for reading contents 
      //of this object
   }

   //allow the base class to serialize along 
   //with its version information
   ar.SerializeClass(RUNTIME_CLASS(CBaseClass));
   CBaseClass::Serialize(ar);
}

Requirements

Header: afx.h

See Also

Reference

CArchive Class

Hierarchy Chart

CArchive::ReadClass

CArchive::WriteClass

CArchive::GetObjectSchema

CArchive::SetObjectSchema

CArchiveException Class

CNotSupportedException Class

Other Resources

CArchive Members