亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch22.htm

?? 24小時學會vc++
?? HTM
?? 第 1 頁 / 共 3 頁
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD>		<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1">	<TITLE>Teach Yourself Visual C++&#174; 5 in 24 Hours -- Hour 22 -- Serialization</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself Visual C++&#174; 5 in 24 Hours</FONT></H1><CENTER><P><A HREF="../ch21/ch21.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch23/ch23.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER"><FONT COLOR="#000077">- Hour 22 -<BR>Serialization</FONT></H1><P>Serialization is the method used by MFC programs to read and write applicationdata to files. In this hour, you will learn about<UL>	<LI>Persistence and serialization	<LI>Serialization support in commonly used MFC classes	<LI>Macros and other MFC features that are used when implementing serialization</UL><P>You will also create an example that uses serialization in a Document/View application.<H2><FONT COLOR="#000077"><B>What Is Serialization?</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B><I>Serialization</I> is theprocess of storing the state of an object for the purpose of loading it at anothertime.</P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The property of an objectto be stored and loaded is <I>persistence</I>, which is also defined as the capabilityof an object to remember its state between executions.</P><P>Serialization is the way in which classes derived from <TT>CDocument</TT> storeand retrieve data from an archive, which is usually a file. Figure 22.1 shows theinteraction between a serialized object and an archive.</P><P><A NAME="01"></A><A HREF="01.htm"><B>Figure 22.1.</B></A> <I><BR>Serializing an object to and from an archive.</I></P><P>When an object is serialized, information about the type of object is writtento the storage along with information and data about the object. When an object isdeserialized, the same process happens in reverse, and the object is loaded and createdfrom the input stream.<H2><FONT COLOR="#000077"><B>Why Use Serialization?</B></FONT></H2><P>The goal behind serialization is to make the storage of complex objects as simpleand reliable as the storage of the basic data types available in C++. You can storea basic type, such as an <TT>int</TT>, in a file in the following way:</P><PRE><FONT COLOR="#0066FF"><TT>int nFoo = 5;</TT><TT>fileStream &lt;&lt; nFoo;</TT></FONT></PRE><P>If a file contains an <TT>int</TT> value, it can be read from the stream in thefollowing way:</P><PRE><FONT COLOR="#0066FF"><TT>fileStream &gt;&gt; nFoo;</TT></FONT></PRE><P>A persistent object can be serialized and deserialized using a similar syntax,no matter how complicated the object's internal structure. The alternative is tocreate routines that understand how every object is implemented and handle the processof storing and retrieving data from files.</P><P>Using serialization to store objects is much more flexible than writing specializedfunctions that store data in a fixed format. Objects that are persistent are capableof storing themselves, instead of relying on an external function to read and writethem to disk. This makes a persistent object much easier to reuse because the objectis more self-contained.</P><P>Persistent objects also help you easily write programs that are saved to storage.An object that is serialized might be made up of many smaller objects that are alsoserialized. Because individual objects are often stored in a collection, serializingthe collection also serializes all objects contained in the collection.<H2><FONT COLOR="#000077"><B>A Document/View Serialization Example</B></FONT></H2><P>Using AppWizard, create an MDI project named Customers. This project uses serializationto store a very simple list of customer names and email addresses, using a persistentclass named <TT>CUser</TT>. This project will serve as the basis for examples andsource code used in the remainder of this hour.<H3><FONT COLOR="#000077"><B>Serializing the Customers Project</B></FONT></H3><P>In Hour 1, &quot;Introducing Visual C++ 5,&quot; you used the insertion operator,or <TT>&lt;&lt;</TT>, to output a value to the screen. This operator is actuallythe C++ left-shift operator, but it is overloaded so that whenever an output objectand variable are separated by a <TT>&lt;&lt;</TT>, as in the following code line,the variable is written to the output object:</P><PRE><FONT COLOR="#0066FF"><TT>file_object &lt;&lt; data</TT></FONT></PRE><P>In a similar way, whenever input is performed and the objects are separated bya <TT>&gt;&gt;</TT>, as in the following code line, a new value for the variableis retrieved from the input object:</P><PRE><FONT COLOR="#0066FF"><TT>file_object &gt;&gt; data</TT></FONT></PRE><P>In C++, unlike some other languages, input and output are controlled by the interactionbetween file and variable objects. The exact process used for input and output iscontrolled by the way in which the classes implement the <TT>&gt;&gt;</TT> and <TT>&lt;&lt;</TT>operators.</P><P>For the topics in this hour, you create a persistent class named <TT>CUser</TT>,along with the helper functions required to serialize a collection of <TT>CUser</TT>objects. Each <TT>CUser</TT> object contains a customer name and email address.<H3><FONT COLOR="#000077"><B>The MFC Classes Used for Serialization</B></FONT></H3><P>You use two MFC classes to serialize objects:<UL>	<LI><TT>CArchive</TT> is almost always a file and is the object that other persistent	objects are serialized to or from.<BR>	<BR>		<LI><TT>CObject</TT> defines all the interfaces used to serialize objects to or from	a <TT>CArchive</TT> object.</UL><P>Objects are serialized in one of two ways. As a rule of thumb, if an object isderived from <TT>CObject</TT>, that object's <TT>Serialize</TT> member function iscalled in the following way:</P><PRE><FONT COLOR="#0066FF"><TT>myObject.Serialize( ar );</TT></FONT></PRE><P>If the object isn't derived from <TT>CObject</TT>--such as a <TT>CRect</TT> object--youshould use the inser-tion operator in the following way:</P><PRE><FONT COLOR="#0066FF"><TT>ar &lt;&lt; rcWnd;</TT></FONT></PRE><P>This insertion operator is overloaded in the same way it is for <TT>cout</TT>,<TT>cin</TT>, and <TT>cerr</TT>, which were used in the first two hours for consolemode input and output.<H4><FONT COLOR="#000077">Using the CObject Class</FONT></H4><P>You must use the <TT>CObject</TT> class for all classes that use the MFC classlibrary's built-in support for serialization. The <TT>CObject</TT> class containsvirtual functions that are used during serialization. In addition, the <TT>CArchive</TT>class is declared as a &quot;friend&quot; class for <TT>CObject</TT>, providing itaccess to private and protected member variables.</P><P>The most commonly used virtual function in <TT>CObject</TT> is <TT>Serialize</TT>,which is called to serialize or deserialize the object from a <TT>CArchive</TT> object.This function is declared as virtual so that any persistent object can be calledthrough a pointer to <TT>CObject</TT> in the following way:</P><PRE><FONT COLOR="#0066FF"><TT>CObject* pObj = GetNextObject();</TT><TT>pObj-&gt;Serialize( ar );</TT></FONT></PRE><P>As discussed later in the section &quot;Using the Serialization Macros,&quot;when you're deriving a persistent class from <TT>CObject</TT>, you must use two macrosto help implement the serialization functions.<H4><FONT COLOR="#000077">The CArchive Class</FONT></H4><P>The <TT>CArchive</TT> class is used to model a generic storage object. In mostcases, a <TT>CArchive</TT> object is attached to a disk file. In some cases, however,the object might be connected to an object that only seems to be a file, like a memorylocation or another type of storage.</P><P>When a <TT>CArchive</TT> object is created, it is defined as used for either inputor output but never both. You can use the <TT>IsStoring</TT> and <TT>IsLoading</TT>functions to determine whether a <TT>CArchive</TT> object is used for input or output,as shown in Listing 22.1.<H4><FONT COLOR="#000077">TYPE: Listing 22.1. Using the CArchive::IsStoring functionto determine the serialization direction.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>CMyObject:Serialize( CArchive&amp; ar )</TT><TT>{</TT><TT>    if( ar.IsStoring() )</TT><TT>        // Write object state to ar</TT><TT>    else</TT><TT>        // Read object state from ar</TT><TT>}</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Using the Insertion and Extraction Operators</B></FONT></H3><P>The MFC class library overloads the insertion and extraction operators for manycommonly used classes and basic types. You often use the insertion operator, <TT>&lt;&lt;</TT>,to serialize--or store--data to the <TT>CArchive</TT> object. You use the extractionoperator, <TT>&gt;&gt;</TT>, to deserialize--or load--data from a <TT>CArchive</TT>object.</P><P>These operators are defined for all basic C++ types, as well as a few commonlyused classes not derived from <TT>CObject</TT>, such as the <TT>CString</TT>, <TT>CRect</TT>,and <TT>CTime</TT> classes. The insertion and extraction operators return a referenceto a <TT>CArchive</TT> object, enabling them to be chained together in the followingway:</P><PRE><FONT COLOR="#0066FF"><TT>archive &lt;&lt; m_nFoo &lt;&lt; m_rcClient &lt;&lt; m_szName;</TT></FONT></PRE><P>When used with classes that are derived from <TT>CObject</TT>, the insertion andextraction operators allocate the memory storage required to contain an object andthen call the object's <TT>Serialize</TT> member function. If you don't need to allocatestorage, you should call the <TT>Serialize</TT> member function directly.</P><P>As a rule of thumb, if you know the type of the object when it is deserialized,call the <TT>Serialize</TT> function directly. In addition, you must always call<TT>Serialize</TT> exclusively. If you use <TT>Serialize</TT> to load or store anobject, you must not use the insertion and extraction operators at any other timewith that object.<H3><FONT COLOR="#000077"><B>Using the Serialization Macros</B></FONT></H3><P>There are two macros that you must use when creating a persistent class basedon <TT>CObject</TT>. Use the <TT>DECLARE_SERIAL</TT> macro in the class declarationfile and the <TT>IMPLEMENT_SERIAL</TT> macro in the class implementation file.<H4><FONT COLOR="#000077">Declaring a Persistent Class</FONT></H4><P>The <TT>DECLARE_SERIAL</TT> macro takes a single parameter: the name of the classto be serialized. An example of a class that can be serialized is provided in Listing22.2. Save this source code in the Customers project directory in a file named <TT>Users.h</TT>.<BLOCKQUOTE>	<P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>A good place to put	the <TT>DECLARE_SERIAL</TT> macro is on the first line of the class declaration,	where it serves as a reminder that the class can be serialized. <HR></BLOCKQUOTE><H4><FONT COLOR="#000077">TYPE: Listing 22.2. The CUser class declaration.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>#ifndef CUSER</TT><TT>#define CUSER</TT><TT>class CUser : public CObject</TT><TT>{</TT><TT>    DECLARE_SERIAL(CUser);</TT><TT>public:</TT><TT>    // Constructors</TT><TT>    CUser();</TT><TT>    CUser( const CString&amp; szName, const CString&amp; szAddr );</TT><TT>    // Attributes</TT><TT>    void Set( const CString&amp; szName, const CString&amp; szAddr );</TT><TT>    CString GetName() const;</TT><TT>    CString GetAddr() const;</TT><TT>    // Operations</TT><TT>    virtual void Serialize( CArchive&amp; ar );</TT><TT>    // Implementation</TT><TT>private:</TT><TT>    // The user's name</TT><TT>    CString m_szName;</TT><TT>    // The user's e-mail addresss</TT><TT>    CString m_szAddr;</TT><TT>};</TT><TT>#endif CUSER</TT></FONT></PRE><H4><FONT COLOR="#000077">Defining a Persistent Class</FONT></H4><P>The <TT>IMPLEMENT_SERIAL</TT> macro takes three parameters and is usually placedbefore any member functions are defined for a persistent class. The parameters for<TT>IMPLEMENT_SERIAL</TT> are the following:<UL>	<LI>The class to be serialized	<LI>The immediate base class of the class being serialized	<LI>The schema, or version number</UL><P>The schema number is a version number for the class layout used when you're serializingand deserializing objects. If the schema number of the data being loaded doesn'tmatch the schema number of the object reading the file, the program throws an exception.The schema number should be incremented when changes are made that affect serialization,such as adding a class member or changing the serialization order.</P><P>The member functions for the <TT>CUser</TT> class, including the <TT>IMPLEMENT_SERIAL</TT>macro, are provided in Listing 22.3. Save this source code in the Customers projectdirectory as <TT>Users.cpp</TT>.<H4><FONT COLOR="#000077">TYPE: Listing 22.3. The CUser member functions.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>#include &quot;stdafx.h&quot;</TT><TT>#include &quot;Users.h&quot;</TT><TT>IMPLEMENT_SERIAL( CUser, CObject, 1 );</TT><TT>CUser::CUser() { }</TT><TT>CUser::CUser( const CString&amp; szName, const CString&amp; szAddr )</TT><TT>{</TT><TT>    Set( szName, szAddr );</TT>

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
一本一道综合狠狠老| 日本一区二区三区国色天香 | 欧美一区二区女人| 中文子幕无线码一区tr | 亚洲综合久久av| 国产成人在线看| 欧美精品成人一区二区三区四区| 亚洲国产精品成人综合| 久久精品99国产精品日本| 欧洲精品视频在线观看| 国产精品黄色在线观看| 国产伦精品一区二区三区免费迷 | 亚洲天堂av一区| 国产乱人伦偷精品视频免下载| 欧美日韩免费高清一区色橹橹| 国产精品―色哟哟| 精品无人区卡一卡二卡三乱码免费卡 | 91美女精品福利| 国产欧美va欧美不卡在线| 精品亚洲国内自在自线福利| 日韩欧美一区二区久久婷婷| 日日摸夜夜添夜夜添精品视频| 欧美日韩一区二区三区四区五区| 亚洲欧美另类综合偷拍| 色哟哟一区二区在线观看| 亚洲色图视频网| 91视频.com| 亚洲一区二区av在线| 欧美自拍偷拍午夜视频| 亚洲精品国产高清久久伦理二区| 99re这里只有精品首页| 亚洲精品ww久久久久久p站| 一本到不卡精品视频在线观看| 亚洲女与黑人做爰| 欧美天天综合网| 天天av天天翘天天综合网| 91精品国产综合久久香蕉麻豆 | 欧美一区二区三区免费| 日本系列欧美系列| 日韩免费成人网| 国产精品538一区二区在线| 国产精品嫩草影院com| 色婷婷综合久久| 图片区小说区区亚洲影院| 精品欧美乱码久久久久久| 国产精品乡下勾搭老头1| 中文字幕一区二区在线观看| 欧美日韩一二三| 激情五月播播久久久精品| 国产精品萝li| 欧美日韩在线三级| 麻豆国产精品官网| 国产精品久久久久精k8 | 亚洲另类在线一区| 欧美丝袜丝nylons| 国产自产高清不卡| 亚洲免费观看在线视频| 制服丝袜亚洲精品中文字幕| 国产尤物一区二区在线| 中文字幕成人在线观看| 欧美日韩中文字幕一区二区| 韩国一区二区三区| 一区二区三区免费| 精品福利一二区| 在线免费不卡视频| 国产一区久久久| 一区二区三区国产豹纹内裤在线| 日韩精品专区在线影院观看| 99国产欧美久久久精品| 久久国产综合精品| 亚洲精品菠萝久久久久久久| 日韩欧美一区二区免费| 在线一区二区三区| 国产一区二区电影| 日本人妖一区二区| 亚洲精品综合在线| 国产网站一区二区| 欧美一区二区观看视频| 色偷偷成人一区二区三区91| 国模套图日韩精品一区二区| 亚洲成a人v欧美综合天堂| 国产精品久久久久久亚洲毛片| 日韩一区二区精品在线观看| 欧美在线三级电影| 丁香一区二区三区| 激情久久久久久久久久久久久久久久| 亚洲青青青在线视频| 久久久噜噜噜久久人人看 | 91国产丝袜在线播放| 激情都市一区二区| 日韩av中文字幕一区二区三区 | 麻豆国产欧美一区二区三区| 亚洲综合一区二区| 最近日韩中文字幕| 欧美激情一区二区三区四区| www亚洲一区| 亚洲精品一区二区三区影院| 欧美日韩mp4| 欧美天天综合网| 欧美在线不卡一区| 色婷婷综合久久久中文一区二区| 粉嫩一区二区三区在线看| 国产一区不卡在线| 国产馆精品极品| 国产成a人亚洲精品| 国产91对白在线观看九色| 国产成人亚洲综合a∨婷婷| 国产在线乱码一区二区三区| 久久成人综合网| 韩国精品在线观看| 91精品国产麻豆| 极品少妇xxxx偷拍精品少妇| 天堂久久久久va久久久久| 香蕉久久夜色精品国产使用方法| 一区二区高清免费观看影视大全| 亚洲精品国产第一综合99久久 | 久久这里都是精品| 日韩精品一区在线| 久久久久99精品一区| 久久久影视传媒| 国产精品欧美久久久久一区二区| 国产精品国产三级国产aⅴ中文 | 久久精品日产第一区二区三区高清版| 欧美成va人片在线观看| 久久精品一区二区三区av| 国产精品网站在线播放| 亚洲另类春色校园小说| 亚洲国产视频一区| 美女高潮久久久| 成人手机电影网| 在线观看一区二区精品视频| 91精品久久久久久久91蜜桃| 精品久久久久久久久久久院品网 | 国产精品毛片a∨一区二区三区| 亚洲视频资源在线| 亚洲成人激情av| 国产一区二区三区日韩| av高清久久久| 欧美卡1卡2卡| 欧美激情综合五月色丁香小说| 亚洲欧洲av色图| 免费欧美在线视频| 成人免费看片app下载| 欧美日韩黄视频| 精品福利在线导航| 亚洲精品日日夜夜| 九色综合狠狠综合久久| 一本一道综合狠狠老| 日韩欧美精品在线| 一区二区三区在线视频观看| 另类综合日韩欧美亚洲| 色悠悠亚洲一区二区| 日韩区在线观看| 亚洲男人的天堂av| 国模套图日韩精品一区二区| 一本到不卡精品视频在线观看| 日韩欧美高清在线| 一区二区久久久久久| 国产激情一区二区三区四区 | 国产一区二区影院| 欧美日本一道本在线视频| 亚洲国产激情av| 裸体健美xxxx欧美裸体表演| 色综合久久中文综合久久97| 精品国产免费人成在线观看| 亚洲综合色噜噜狠狠| 成人免费视频一区二区| 精品国产一区二区亚洲人成毛片| 一区二区三区中文免费| 成人免费看片app下载| 精品久久一区二区| 日韩影院在线观看| 91黄色免费看| √…a在线天堂一区| 国产a精品视频| 久久精品亚洲麻豆av一区二区| 日韩高清在线一区| 欧美精品xxxxbbbb| 亚洲一区二区三区四区在线| 99久免费精品视频在线观看| www国产亚洲精品久久麻豆| 免费视频最近日韩| 欧美浪妇xxxx高跟鞋交| 亚洲图片欧美综合| 色呦呦日韩精品| 亚洲乱码国产乱码精品精的特点| 成人av一区二区三区| 久久精品人人做人人综合 | 国产精品二三区| 国产成人精品亚洲日本在线桃色 | 精品少妇一区二区三区| 午夜一区二区三区在线观看| 欧美亚洲国产怡红院影院| 亚洲少妇30p| 一本到一区二区三区| ㊣最新国产の精品bt伙计久久| 丁香婷婷综合激情五月色| 国产精品丝袜一区| 成人av电影免费在线播放| 日韩一区中文字幕|