zoukankan      html  css  js  c++  java
  • vc++上的MFC的对象序列化和反序列化

     

    注意点:
     1. 必须类型序列化声明
        DECLARE_SERIAL( Person )
     
     2. 必须写出实现宏
     IMPLEMENT_SERIAL(Person, CObject, VERSIONABLE_SCHEMA | 2)
     
     3. 重写CObject中的Serialize函数
     void Person::Serialize( CArchive& ar )
     {
      CObject::Serialize(ar);
      //关键代码
      if(ar.IsStoring()) {
       //序列化
       ar << this->age << this->sex << this->name;
      } else {
       //反序列化
       ar >> this->age >> this->sex >> this->name;
      }
     }

    序列化后的数据

    [cpp] view plaincopy
     
    1. //Person.h  
    2. #pragma once  
    3. #include <afx.h>  
    4. #include <string>  
    5. #include <atlstr.h>  
    6.   
    7. using namespace std;  
    8.   
    9. class Person: public CObject  
    10. {  
    11. private:  
    12.     //注意MFC 不支持 标准std:string对象序列化, boost库支持std:string       
    13.     CString name;  
    14.     int age;  
    15.     char sex;  
    16. public:  
    17.     DECLARE_SERIAL( Person )  
    18.   
    19.     Person(void);  
    20.   
    21.     Person(CString name, int age, char sex);  
    22.   
    23.     virtual ~Person(void);  
    24.   
    25.     virtual void Serialize(CArchive& ar);  
    26.   
    27.     void setName(CString pName);  
    28.   
    29.     CString getName();  
    30.   
    31.     void setAge(int age);  
    32.   
    33.     int getAge();  
    34.   
    35.     void setSex(char sex);  
    36.   
    37.     char getSex();  
    38. };  
    39.   
    40. //Person.cpp  
    41. #include "StdAfx.h"  
    42. #include "Person.h"  
    43. #include <afx.h>  
    44. #include <string>  
    45.   
    46. //必须写出实现宏  
    47. IMPLEMENT_SERIAL(Person, CObject, VERSIONABLE_SCHEMA | 2)  
    48.   
    49. Person::Person(void)  
    50. {  
    51. }  
    52.   
    53. Person::Person( CString name, int age, char sex )  
    54. {  
    55.     this->name = name;  
    56.     this->age = age;  
    57.     this->sex = sex;  
    58. }  
    59.   
    60. Person::~Person(void)  
    61. {  
    62. }  
    63.   
    64. void Person::setName(  CString name)  
    65. {  
    66.     this->name = name;  
    67. }  
    68.   
    69. CString Person::getName()  
    70. {  
    71.     return this->name;  
    72. }  
    73.   
    74. void Person::setAge( int age )  
    75. {  
    76.     this->age = age;  
    77. }  
    78.   
    79. int Person::getAge()  
    80. {  
    81.     return this->age;  
    82. }  
    83.   
    84. void Person::setSex( char sex )  
    85. {  
    86.     this->sex = sex;  
    87. }  
    88.   
    89. char Person::getSex()  
    90. {  
    91.     return this->sex;  
    92. }  
    93.   
    94. void Person::Serialize( CArchive& ar )  
    95. {  
    96.     CObject::Serialize(ar);  
    97.     //关键代码  
    98.     if(ar.IsStoring()) {  
    99.         //序列化  
    100.         ar << this->age << this->sex << this->name;  
    101.     } else {  
    102.         //反序列化  
    103.         ar >> this->age >> this->sex >> this->name;  
    104.     }  
    105. }  
    106.   
    107. // main.cpp : 定义控制台应用程序的入口点。  
    108. #include "stdafx.h"  
    109. #include <tchar.h>  
    110. #include <afx.h>  
    111. #include <iostream>  
    112.   
    113. using namespace std;  
    114.   
    115. int _tmain(int argc, _TCHAR* argv[])  
    116. {  
    117.     Person person;  
    118.     person.setAge(20);  
    119.     person.setName("zhangsan");  
    120.     person.setSex('1');  
    121.   
    122.     CFile myFile(_T("c:/person.ser"), CFile::modeCreate | CFile::modeReadWrite);  
    123.     // Create a storing archive.  
    124.     CArchive arStore(&myFile, CArchive::store);  
    125.   
    126.     // Write the object to the archive  
    127.     arStore.WriteObject(&person);  
    128.   
    129.     arStore.Flush();  
    130.     // Close the storing archive  
    131.     arStore.Close();  
    132.   
    133.     // Create a loading archive.  
    134.     myFile.SeekToBegin();  
    135.     CArchive arLoad(&myFile, CArchive::load);  
    136.   
    137.     // Verify the object is in the archive.  
    138.     Person* p = (Person*) arLoad.ReadObject(person.GetRuntimeClass());  
    139.     arLoad.Close();  
    140.   
    141.     //wcout << "姓名:" << name.GetBuffer(name.GetLength()) << endl;  
    142.   
    143.     CString name = p->getName();  
    144.     wchar_t* pch = name.GetBuffer(0);  
    145.     wcout << "姓名:" << pch << endl;  
    146.     name.ReleaseBuffer(); //注意内在释放  
    147.   
    148.     cout << "性别:" << p->getSex() << endl;  
    149.     cout << "年龄:" << p->getAge() << endl;  
    150.   
    151.     delete p;  
    152.     return 0;  
    153. }  
  • 相关阅读:
    ThreadLocal
    mysql
    heroku 的用法
    Redis
    disruptor
    RxJava
    TCP
    虚拟机的安装及配置等
    k8s
    Ribbon源
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/4611291.html
Copyright © 2011-2022 走看看