亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
亚洲综合久久久| 青青草国产精品97视觉盛宴 | 开心九九激情九九欧美日韩精美视频电影| 精品国产三级a在线观看| 91婷婷韩国欧美一区二区| 日本在线观看不卡视频| 国产精品狼人久久影院观看方式| 在线播放欧美女士性生活| 色综合久久中文字幕| 国产真实乱子伦精品视频| 亚洲一二三四久久| 国产精品视频免费| 日韩美女视频在线| 欧美另类久久久品| 欧美亚州韩日在线看免费版国语版| 国产美女一区二区| 久久er精品视频| 日韩高清一区二区| 亚洲国产aⅴ成人精品无吗| 国产精品护士白丝一区av| 久久久久久免费网| 欧美videossexotv100| 欧美色综合久久| 欧美中文字幕一区二区三区 | 国产真实乱子伦精品视频| 日韩精品免费视频人成| 亚洲午夜精品一区二区三区他趣| 国产精品欧美久久久久一区二区| 久久婷婷综合激情| 精品成人佐山爱一区二区| 制服丝袜一区二区三区| 欧美日韩国产首页在线观看| 色久优优欧美色久优优| 99久久99久久综合| 99精品视频在线免费观看| 成人免费毛片app| 国产**成人网毛片九色| 国产mv日韩mv欧美| 国产电影一区二区三区| 高清免费成人av| www.成人在线| 色综合色综合色综合| 一本久久综合亚洲鲁鲁五月天 | 欧美国产97人人爽人人喊| 久久久久久麻豆| 久久久精品国产免大香伊| 国产午夜精品在线观看| 国产亚洲成aⅴ人片在线观看| 国产免费成人在线视频| 国产精品午夜电影| 一区二区三区不卡视频| 亚洲午夜电影在线| 日本成人中文字幕在线视频| 日韩精品色哟哟| 国产一区二区三区在线观看精品 | 五月婷婷另类国产| 青草国产精品久久久久久| 久久不见久久见中文字幕免费| 麻豆极品一区二区三区| 国产精品亚洲视频| av午夜精品一区二区三区| 日本电影欧美片| 777xxx欧美| 久久久久成人黄色影片| 中文字幕综合网| 性做久久久久久免费观看| 久久se精品一区二区| 高清成人在线观看| 欧美亚洲精品一区| 欧美videos大乳护士334| 国产精品美女视频| 亚洲一区在线看| 蜜臀av一区二区三区| 成人18精品视频| 欧美无人高清视频在线观看| 欧美videossexotv100| 亚洲私人影院在线观看| 日韩高清不卡在线| 成人久久18免费网站麻豆 | 一区二区久久久| 亚洲一二三四区| 国产在线精品免费| 精品在线亚洲视频| 香蕉久久一区二区不卡无毒影院 | 午夜精品一区二区三区三上悠亚| 欧美三级一区二区| 日韩精品中午字幕| 国产精品久久久久久妇女6080| 一个色综合网站| 狠狠色综合日日| 在线观看精品一区| 久久综合色综合88| 亚洲大片免费看| 成人晚上爱看视频| 欧美一卡2卡3卡4卡| 国产日韩精品一区二区三区| 久久99国产精品尤物| 91香蕉视频在线| 欧美xxxxx牲另类人与| 国产日韩欧美制服另类| 亚洲mv在线观看| 成人动漫中文字幕| 日韩区在线观看| 亚洲成人免费av| 色诱亚洲精品久久久久久| 精品99999| 日本强好片久久久久久aaa| 91免费看视频| 国产精品久久午夜夜伦鲁鲁| 国产在线精品一区二区三区不卡| 欧美色精品在线视频| 亚洲人吸女人奶水| 色综合天天综合网天天狠天天| 一区二区三区久久| 激情av综合网| 亚洲色图视频网站| 福利视频网站一区二区三区| 欧美性大战久久| 国产三级精品视频| 国产精品综合av一区二区国产馆| 欧美日韩精品一区二区三区蜜桃 | 亚洲免费高清视频在线| 国产精品综合一区二区三区| 欧美剧情电影在线观看完整版免费励志电影| 中文字幕在线观看不卡| 激情文学综合插| 精品国产乱码久久久久久影片| 日韩精品三区四区| 欧美日韩一区小说| 亚洲小少妇裸体bbw| 91麻豆国产在线观看| 中文字幕在线观看不卡| 成人av资源在线| 国产精品欧美极品| 成人av综合一区| 中文字幕一区二区在线观看| 丁香激情综合五月| 欧美国产精品久久| 成人h动漫精品| 国产精品电影院| 99久久99久久精品免费观看| 国产精品家庭影院| 91免费观看在线| 亚洲自拍欧美精品| 欧美日韩不卡一区二区| 五月天婷婷综合| 日韩欧美美女一区二区三区| 美女免费视频一区| 欧美精品一区二| 国产成人av资源| 亚洲欧美偷拍卡通变态| 在线免费亚洲电影| 亚洲成人在线观看视频| 91精品黄色片免费大全| 免费一级欧美片在线观看| 精品国产露脸精彩对白| 粉嫩一区二区三区在线看| 国产精品成人在线观看| 在线一区二区三区四区| 日韩精品成人一区二区三区| 日韩免费性生活视频播放| 国产一区二区三区四区五区美女 | 亚洲国产综合人成综合网站| 欧美日韩国产高清一区| 麻豆国产精品一区二区三区| 久久精品网站免费观看| 91免费观看视频| 蜜桃av一区二区| 2020国产精品| 99国内精品久久| 免费xxxx性欧美18vr| 国产亚洲欧美日韩俺去了| 日本高清不卡aⅴ免费网站| 免费在线看一区| 亚洲日本在线观看| 欧美一级理论片| av日韩在线网站| 日本不卡中文字幕| 国产精品福利一区二区| 欧美日本一区二区三区四区| 国产精品一二三| 亚洲bt欧美bt精品| 国产清纯白嫩初高生在线观看91 | 蜜桃视频一区二区三区在线观看| 国产三级久久久| 欧美精品在线一区二区| 国产91丝袜在线18| 日本午夜一区二区| 亚洲色图欧美在线| 精品国产乱码久久| 在线观看91精品国产入口| 黄色资源网久久资源365| 亚洲欧美国产毛片在线| 欧美精品一区二区三区蜜臀| 色偷偷久久一区二区三区| 久久99精品国产麻豆婷婷洗澡| **欧美大码日韩| 久久精品视频免费观看| 在线播放一区二区三区| 91污在线观看|