mfc只有doc才能序列化吗_MFC 中的序列化

MFC 中的序列化Serialization in MFC

11/04/2016

本文内容

本文介绍了 Microsoft 基础类库 (MFC) 中提供的序列化机制,以允许对象在程序运行之间保持不变。This article explains the serialization mechanism provided in the Microsoft Foundation Class Library (MFC) to allow objects to persist between runs of your program.

序列化是在持久存储介质(如磁盘文件)中写入或读取对象的过程。Serialization is the process of writing or reading an object to or from a persistent storage medium such as a disk file. 序列化适用于需要在程序执行期间或之后维护结构化数据 ((如 c + + 类或结构) )的状态的情况。Serialization is ideal for situations where it is desired to maintain the state of structured data (such as C++ classes or structures) during or after execution of a program. 如果使用 MFC 提供的序列化对象,则可以以标准且一致的方式进行此操作,从而使用户不必手动执行文件操作。Using the serialization objects provided by MFC allows this to occur in a standard and consistent manner, relieving the user from the need to perform file operations by hand.

MFC 为类中的序列化提供内置支持 CObject 。MFC supplies built-in support for serialization in the class CObject. 因此,从派生的所有类 CObject 都可以利用 CObject 的序列化协议。Thus, all classes derived from CObject can take advantage of CObject's serialization protocol.

序列化的基本思路是,对象应该能够将其当前状态(通常由其成员变量的值指示)写入持久存储。The basic idea of serialization is that an object should be able to write its current state, usually indicated by the value of its member variables, to persistent storage. 稍后,可以通过从存储区中读取或反序列化对象的状态,重新创建对象。Later, the object can be re-created by reading, or deserializing, the object's state from the storage. 序列化处理对象指针的所有详细信息以及对序列化对象时所使用的对象的循环引用。Serialization handles all the details of object pointers and circular references to objects that are used when you serialize an object. 关键点在于对象本身负责读取和写入其自己的状态。A key point is that the object itself is responsible for reading and writing its own state. 因此,要使类可序列化,它必须实现基本的序列化操作。Thus, for a class to be serializable, it must implement the basic serialization operations. 如序列化项目组中所示,可以轻松地将此功能添加到类中。As shown in the Serialization group of articles, it is easy to add this functionality to a class.

MFC 使用类的对象 CArchive 作为要序列化的对象和存储介质之间的中介。MFC uses an object of the CArchive class as an intermediary between the object to be serialized and the storage medium. 此对象始终与 CFile 对象关联,从中获取序列化所需的信息,包括文件名以及请求的操作是读取还是写入。This object is always associated with a CFile object, from which it obtains the necessary information for serialization, including the file name and whether the requested operation is a read or write. 执行序列化操作的对象可以使用对象, CArchive 而不考虑存储介质的本质。The object that performs a serialization operation can use the CArchive object without regard to the nature of the storage medium.

CArchive对象使用重载插入 (<>) 运算符来执行写入和读取操作。A CArchive object uses overloaded insertion (<>) operators to perform writing and reading operations. 有关详细信息,请参阅文章序列化:序列化对象中的 通过存档存储和加载 cobject 。For more information, see Storing and Loading CObjects via an Archive in the article Serialization: Serializing an Object.

备注

不要将 CArchive 类与通用 iostream 类(仅用于格式化文本)混淆。Do not confuse the CArchive class with general-purpose iostream classes, which are for formatted text only. CArchive类适用于二进制格式序列化的对象。The CArchive class is for binary-format serialized objects.

如果需要,可以跳过 MFC 序列化,为永久性数据存储创建自己的机制。If you want, you can bypass MFC serialization to create your own mechanism for persistent data storage. 你将需要在用户的命令中重写启动序列化的类成员函数。You will need to override the class member functions that initiate serialization at the user's command. 请参阅 ID_FILE_OPEN、ID_FILE_SAVE 和 ID_FILE_SAVE_AS 标准命令的 技术说明 22 中的讨论。See the discussion in Technical Note 22 of the ID_FILE_OPEN, ID_FILE_SAVE, and ID_FILE_SAVE_AS standard commands.

以下文章介绍了序列化所需的两项主要任务:The following articles cover the two main tasks required for serialization:

序列化 :序列化与数据库输入/输出 一文介绍了在数据库应用程序中,序列化是适当的输入/输出技术。The article Serialization: Serialization vs. Database Input/Output describes when serialization is an appropriate input/output technique in database applications.

请参阅See also

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值