亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
日韩在线一区二区| 欧美日韩专区在线| 欧美午夜一区二区三区| 26uuu国产在线精品一区二区| 国产精品福利在线播放| 久久爱www久久做| 欧美体内she精视频| 日本一区二区动态图| 激情文学综合丁香| 欧美日韩综合在线| 亚洲欧美日韩国产手机在线| 成人午夜在线视频| 欧美xfplay| 日韩影院在线观看| 欧美性videosxxxxx| 亚洲欧美日韩成人高清在线一区| 国产成人av电影在线| 欧美mv和日韩mv国产网站| 亚洲高清不卡在线观看| 色偷偷久久人人79超碰人人澡 | 日韩精品免费专区| 欧美丝袜自拍制服另类| 亚洲欧美综合网| 99免费精品视频| 日韩毛片精品高清免费| 国产aⅴ精品一区二区三区色成熟| 精品人在线二区三区| 免费观看日韩电影| 国产精品少妇自拍| 国产盗摄精品一区二区三区在线 | 欧美午夜片在线看| 亚洲一二三四区不卡| 欧美综合久久久| 亚洲成人在线免费| 91精品久久久久久久99蜜桃| 三级在线观看一区二区| 这里只有精品视频在线观看| 日韩精品国产精品| 日韩免费视频一区二区| 国产一区在线看| 欧美激情综合在线| 色悠久久久久综合欧美99| 亚洲一区二区三区三| 欧美日本高清视频在线观看| 日韩av一二三| 久久精品一区二区三区av| 欧美色图免费看| 无吗不卡中文字幕| 精品欧美黑人一区二区三区| 成人夜色视频网站在线观看| 亚洲色图.com| 欧美日韩在线亚洲一区蜜芽| 麻豆久久久久久| 欧美国产日本韩| 在线观看日韩电影| 韩国三级中文字幕hd久久精品| 国产人妖乱国产精品人妖| 97久久精品人人爽人人爽蜜臀| 亚洲成人自拍一区| 久久精品人人做人人爽人人| youjizz国产精品| 亚洲国产wwwccc36天堂| 久久老女人爱爱| 日本高清不卡一区| 久久99深爱久久99精品| 国产精品久久一卡二卡| 久久人人爽爽爽人久久久| 不卡在线视频中文字幕| 日日夜夜免费精品| 国产精品理论片| 欧美一区二区网站| av一二三不卡影片| 免费观看一级欧美片| 国产精品不卡一区二区三区| 欧美精品乱人伦久久久久久| 国产成人午夜视频| 日韩精品免费视频人成| 最新成人av在线| 精品久久一区二区三区| 91久久一区二区| 国产一区二三区| 欧美成人精精品一区二区频| 国产精品国产三级国产aⅴ入口| 欧美日韩日日夜夜| 一本到不卡免费一区二区| 久久不见久久见免费视频7| 亚洲一区在线观看网站| 国产精品色哟哟| 日韩欧美中文字幕精品| 欧美裸体bbwbbwbbw| 99久久精品免费看| 国产九色sp调教91| 麻豆国产精品777777在线| 亚洲黄色性网站| 综合激情成人伊人| 国产精品婷婷午夜在线观看| 久久综合一区二区| 精品日韩av一区二区| 欧美一级日韩一级| 欧美三级电影网站| 欧美做爰猛烈大尺度电影无法无天| 大陆成人av片| 国产aⅴ精品一区二区三区色成熟| 久久99久久99精品免视看婷婷 | 日韩黄色免费电影| 亚洲国产一区二区三区| 亚洲影院免费观看| 亚洲精品精品亚洲| 亚洲免费高清视频在线| 中文字幕日韩一区| 亚洲人午夜精品天堂一二香蕉| 欧美激情一区在线| 国产精品丝袜在线| 国产精品色婷婷久久58| 国产精品久久久久桃色tv| 国产欧美精品一区二区三区四区| 国产亚洲欧美一级| 日本一区二区高清| 亚洲欧美日韩在线播放| 一区二区三区四区在线播放 | 欧美午夜精品久久久| 欧美日本免费一区二区三区| 欧美体内she精高潮| 3atv在线一区二区三区| 日韩亚洲欧美综合| 综合久久综合久久| 一区二区三区中文在线| 偷拍一区二区三区| 麻豆精品久久久| 国产精品888| 99精品在线免费| 欧美少妇bbb| 日韩精品一区二区三区视频| www久久精品| 亚洲欧洲精品一区二区三区不卡| 亚洲精品日韩一| 日韩激情一二三区| 国产成人av资源| 91国模大尺度私拍在线视频 | 日本精品裸体写真集在线观看| 欧美日韩综合在线免费观看| 91精品国产一区二区人妖| 国产无遮挡一区二区三区毛片日本| 国产精品乱码妇女bbbb| 亚洲永久免费视频| 国产在线精品一区二区| 91久久香蕉国产日韩欧美9色| 日本一区二区三区免费乱视频| 亚洲视频在线一区观看| 天堂久久一区二区三区| 粉嫩一区二区三区性色av| 欧美日韩三级在线| 欧美极品少妇xxxxⅹ高跟鞋 | www.亚洲人| 欧美午夜一区二区| 国产色一区二区| 亚洲aⅴ怡春院| 国产99久久精品| 欧美久久久一区| 成人欧美一区二区三区在线播放| 日韩精品视频网站| 91网址在线看| 久久久蜜桃精品| 免费在线一区观看| 色菇凉天天综合网| 亚洲国产成人一区二区三区| 五月激情综合婷婷| 91色综合久久久久婷婷| 亚洲三级久久久| 国内精品久久久久影院色| 欧洲一区在线观看| 国产精品狼人久久影院观看方式| 麻豆中文一区二区| 欧美自拍偷拍一区| 亚洲人成伊人成综合网小说| 国产乱码精品1区2区3区| 欧美精品 国产精品| 亚洲毛片av在线| av电影在线不卡| 亚洲国产精品99久久久久久久久| 久久99久久99小草精品免视看| 欧美日本一区二区| 一区二区在线免费观看| 不卡影院免费观看| 中文文精品字幕一区二区| 久久91精品国产91久久小草| 91精品国模一区二区三区| 午夜日韩在线观看| 在线观看视频91| 亚洲在线免费播放| 欧美天堂一区二区三区| 亚洲自拍另类综合| 在线中文字幕不卡| 青娱乐精品在线视频| 欧美精品一二三四| 日韩综合在线视频| 69久久99精品久久久久婷婷| 日韩精品亚洲一区二区三区免费| 欧美午夜精品一区二区蜜桃| 一级女性全黄久久生活片免费|