Implement multiple types of serialization

Source: Internet
Author: User

Another point is that the recovery from the file ----> memory ---------> object is not called by the constructor. This is amazing...
We successfully restored two objects but did not call the constructor... the reason why I succeeded in this serialization method is that the compiler has implemented a certain method.
Support. So I said that the serialization method of Microsoft MFC may be this way...
 

 


# Include <iostream>
# Include <fstream>
# Include <string>
Using namespace std;
Class Base
{
Public:
/* The Base type provides a unified implementation. All classes to be serialized must inherit from the Base class, including member objects.
* LoadObject does not require the polymorphism mechanism to be implemented independently as a function.
* Length is the actual length of the object. In order to avoid wasting space in memory reallocation
* Long length = sizeof (* obj); // This is not allowed because we find that the sizeof operator is actually used.
* During the operation, the size of the object indicated by the current runtime type is obtained. if the object is transformed up, it will be narrowed to 1. Therefore, the actual size of www.2cto.com must be passed here.
* LoadObject provides serialization and deserialization functions as the Base's static methods respectively.
*/
Static void StoreObject (Base * obj, string file, int length)
{
Cout <"Write object to file..." <endl;
Ofstream * o = new ofstream (file. c_str ());
O-> write (char *) obj, length );
O-> close ();
Delete o;
}
Static Base * LoadObject (string file, int length)
{
Cout <"Restore objects from files..." <endl;
If (Base: tem_object! = NULL)
Return Base: tem_object;
Char * buf = new char [length];
Ifstream I (file. c_str ());
I> buf;
Base * p = (Base *) buf;
Base: tem_object = p;
I. close ();
Return (p );
}
/*
* The constructor performs initialization operations.
*/
Base ()
{

Cout <"base class constructor..." <endl ;}
/*
* Release space allocated during deserialization
*/
Virtual ~ Base ()
{
If (NULL = Base: tem_object)
Delete Base: tem_object;
Base: tem_object = NULL;
}
Private:
/*
* For Objects temporarily loaded, the same object is always loaded no matter how many times it is loaded.
* The same memory returned is the same object. After the object declaration cycle is completed, re-loading will be different objects.
*/
Static Base * tem_object;
};
Base * Base: tem_object = NULL; // The static pointer must be initialized.
// Serialize Data
Class Data: public Base
{
Private:
Int data;
Public:
Data (int x)
{
Cout <"Data construction..." <endl;
This-> data = x;
}
Void OutPut ()
{
Cout <"Data. data =" <this-> data <endl;
}
};
// Serialization class MyObject
Class MyObject: public Base
{
Public:
MyObject (int x)
{
Cout <"MyObject construction..." <endl;
This-> x = x;
}
~ MyObject ()
{
}
 
MyObject ShowX () // The simplest code
{
Cout <"x =" <x <endl;
Return (* this );
}
Private:
Int x;
};
Int main ()
{
String file1 = "c :\\ data.txt ";
String file2 = "c :\\ myobject.txt ";
Int len1 = sizeof (Data );
Int len2 = sizeof (MyObject );
/*
* Serialization Test
*/
// Test object 1...
Data * d = new Data (8 );
Base: StoreObject (d, file1, len1 );
// Test object 2
MyObject * obj = new MyObject (33 );
Base: StoreObject (obj, file2, len2 );

/*
* Deserialization test ..
*/
(Data *) Base: LoadObject (file1, len1)-> OutPut ();
(MyObject *) Base: LoadObject (file2, len2)-> ShowX ();

Delete d;
Delete obj;
Return 0;
}
A bug occurs before you go to bed. You have to take a bare test of the BUG tomorrow ..
BUG1 serializes more than two objects at the same time. If serialization and deserialization are put together, it is like the following code:
Data * d = new Data (43 );
Base: StoreObject (d, file1, len1 );
MyObject * obj = new MyObject (13 );
Base: StoreObject (obj, file2, len2 );
(Data *) Base: LoadObject (file1, len1)-> OutPut ();
(MyObject *) Base: LoadObject (file2, len2)-> ShowX ();
It is found that the value of x in the Data class is the same as that in the MyObject class... However, if one of the following operations is processed, there will be no problem.
Data * d = new Data (43 );
Base: StoreObject (d, file1, len1 );
(Data *) Base: LoadObject (file1, len1)-> OutPut ();
If you call the following
Data * d = new Data (43 );
Base: StoreObject (d, file1, len1 );
Then, comment out the above Code. when calling the following code, garbled values will appear during loading from the file.
(Data *) Base: LoadObject (file1, len1)-> OutPut ();
On the contrary, if serialization and deserialization are written together at one time, there will be no problem even if the next call is made multiple times ,,,
After the test, continue debugging... Which one is willing to help Debug...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.