Serialization: Making a Serializable Class

Five main steps are required to make a class serializable. They are listed below and explained in the following sections:

  1. Deriving your class from CObject (or from some class derived from CObject).

  2. Overriding the Serialize member function.

  3. Using the DECLARE_SERIAL macro in the class declaration.

  4. Defining a constructor that takes no arguments.

  5. Using the IMPLEMENT_SERIAL macro in the implementation file for your class.

If you call Serialize directly rather than through the >> and << operators of CArchive, the last three steps are not required for serialization.

Deriving Your Class from CObject

The basic serialization protocol and functionality are defined in the CObject class. By deriving your class from CObject (or from a class derived from CObject), as shown in the following declaration of class CPerson, you gain access to the serialization protocol and functionality of CObject.

Overriding the Serialize Member Function

The Serialize member function, which is defined in the CObject class, is responsible for actually serializing the data necessary to capture an object's current state. The Serialize function has a CArchive argument that it uses to read and write the object data. The CArchive object has a member function, IsStoring, which indicates whether Serialize is storing (writing data) or loading (reading data). Using the results of IsStoring as a guide, you either insert your object's data in the CArchive object with the insertion operator (<<) or extract data with the extraction operator (>>).

Consider a class that is derived from CObject and has two new member variables, of types CString and WORD. The following class declaration fragment shows the new member variables and the declaration for the overridden Serialize member function:

class  CPerson :  public  CObject
{
public :
    DECLARE_SERIAL( CPerson )
    
//  empty constructor is necessary
    CPerson(){};

    CString m_name;
    WORD   m_number;

    
void  Serialize( CArchive &  archive );
    
    
//  rest of class declaration
};

 

To override the Serialize member function

  1. Call your base class version of Serialize to make sure that the inherited portion of the object is serialized.

  2. Insert or extract the member variables specific to your class.

    The insertion and extraction operators interact with the archive class to read and write the data. The following example shows how to implement Serialize for the CPerson class declared above:

  3. void  CPerson::Serialize( CArchive &  archive )
    {
        
    //  call base class function first
        
    //  base class is CObject in this case
        CObject::Serialize( archive );

        
    //  now do the stuff for our specific class
         if ( archive.IsStoring() )
            archive 
    <<  m_name  <<  m_number;
        
    else
            archive 
    >>  m_name  >>  m_number;
    }

 

转载于:https://www.cnblogs.com/NeuqUstcIim/archive/2008/08/20/1272281.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值