Armazenar e carregando CObjects por meio de um arquivar

Armazenar e carregando CObjects por meio de um arquivar requer considerações extra. Em determinados casos, você deve chamar o Serialize função de objeto, onde o CArchive objeto é um parâmetro das Serialize telefonar, em oposição a usar o << or >> operador de do CArchive. The important fact to keep in mind is that the CArchive >> operator constructs the CObject in memory based on CRuntimeClass information previously written to the file by the storing archive.

Therefore, whether you use the CArchive << and >> operators, versus calling Serialize, depends on whether you need the loading archive to dynamically reconstruct the object based on previously stored CRuntimeClass information.Use o Serialize função nos seguintes casos:

  • Ao desserializar o objeto, saber antecipadamente a classe exato do objeto.

  • Ao desserializar o objeto, você já tem memória alocada para ele.

Cuidado:

Se você carregar o objeto usando o Serialize função, você deve também armazenar o objeto usando o Serialize função. Don't store using the CArchive << operator and then load using the Serialize function, or store using the Serialize function and then load using CArchive >> operator.

O exemplo a seguir ilustra os casos:

class CMyObject : public CObject
{
// ...Member functions
public:
   CMyObject() { }
   virtual void Serialize( CArchive& ar );

// Implementation
protected:
   DECLARE_SERIAL( CMyObject )
};


class COtherObject : public CObject
{
   // ...Member functions
public:
   COtherObject() { }
   virtual void Serialize( CArchive& ar );

// Implementation
protected:
   DECLARE_SERIAL( COtherObject )
};


class CCompoundObject : public CObject
{
   // ...Member functions
public:
   CCompoundObject();
   ~CCompoundObject();
   virtual void Serialize( CArchive& ar );

// Implementation
protected:
   CMyObject m_myob;    // Embedded object
   COtherObject* m_pOther;    // Object allocated in constructor
   CObject* m_pObDyn;    // Dynamically allocated object
   //..Other member data and implementation

   DECLARE_SERIAL( CCompoundObject )
};
IMPLEMENT_SERIAL(CMyObject,CObject,1)
IMPLEMENT_SERIAL(COtherObject,CObject,1)
IMPLEMENT_SERIAL(CCompoundObject,CObject,1)


CCompoundObject::CCompoundObject()
{
   m_pOther = new COtherObject; // Exact type known and object already 
            //allocated.
   m_pObDyn = NULL;    // Will be allocated in another member function
            // if needed, could be a derived class object.
}

CCompoundObject::~CCompoundObject()
{
   delete m_pOther;
}

void CCompoundObject::Serialize( CArchive& ar )
{
   CObject::Serialize( ar );    // Always call base class Serialize.
   m_myob.Serialize( ar );    // Call Serialize on embedded member.
   m_pOther->Serialize( ar );    // Call Serialize on objects of known exact type.

   // Serialize dynamic members and other raw data
   if ( ar.IsStoring() )
   {
      ar << m_pObDyn;
      // Store other members
   }
   else
   {
      ar >> m_pObDyn; // Polymorphic reconstruction of persistent object 
      //load other members
   }
}

In summary, if your serializable class defines an embedded CObject as a member, you should not use the CArchive << and >> operators for that object, but should call the Serialize function instead.Além disso, se sua classe serializável define um ponteiro para um CObject (ou um objeto derivado de CObject) sistema autônomo um membro, mas construções esse Outros objeto em seu próprio construtor, você deve também telefonar Serialize.

Consulte também

Conceitos

Serialização: Serializando um objeto