你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Linux开发
串行化(Serialization)(5)
 
对象在CDocument派生类中的保存和文件I/O过程

  为了简化设计,在CDocument类派生类中,采用MFC提供的模板类CPtrList来保存对象。该对象定义为:

    protected:
      CTypedPtrList m_listPictures;

  由于CTypedPtrList和CPtrList都没有实现Serialize函数,因此不能够通过ar << m_listPictures和ar >> m_listPictures 来序列化对象,因此CPictureDoc的Serialize函数需要如下实现:

    void CTsDoc::Serialize(CArchive& ar)
    {
      POSITION pos;
      if (ar.IsStoring())
      {
        // TODO: add storing code here
        pos = m_listPictures.GetHeadPosition();
        while(pos != NULL)
        {
          ar << m_listPictures.GetNext (pos);
        }
      }
      else
      {
        // TODO: add loading code here
        RemoveAll();
        CPicture * pPicture;
        do{
          try
          {
            ar >> pPicture;
            TRACE("Read Object %d
",pPicture->GetType ());
            m_listPictures.AddTail(pPicture);
          }
          catch(CException * e)
          {
            e->Delete ();
            break;
          }
        }while(pPicture != NULL);
      }
      m_pCurrent = NULL;
      SetModifiedFlag(FALSE);
    }
实现派生类的串行化功能

  几何图形程序支持直线、矩形、三角形、椭圆等图形,分别以类CLine、CRectangle、CTriangle和CEllipse实现。以类CLine为例,实现串行化功能:

  从CPicture派生CLine,在CLine类定义中增加如下成员变量:  CPoint m_ptStart,m_ptEnd;

  在该行下一行增加如下宏:  DECLARE_SERIAL(CLine)

(编辑:aniston)

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089