我的思路是内存,因为我们知道我们所有的数据都是存储在内存中的二进制数据 。 那么可以知道 我们的程序中的类的对象在运行过程中也相应的在他进城的存储
空间中对应一段二进制数据,那么我们是否可以将这个数据保存到本地然后从本地加载,我就兴奋的去尝试了,下 果然可以....原来对象序列化的原理是如此简单:
说道这里下面代码只完成了 简单的包含基本数据类型和方法的类进行序列化 ...经验证可以实现简单序列化 。。。
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std ;
- template <class T>
- class MyObject
- {
- public :
- MyObject(T x)
- {
- cout<<"对象构造中!"<<endl ;
- this->x=x ;
- }
- ~MyObject()
- {
- }
- static void StoreObject(MyObject *obj)
- {
- long length=sizeof(*obj) ;
- ofstream *o=new ofstream("c:\\obj.xxx") ;
- o->write((char*)obj,length) ;
- o->close() ;
- delete o ;
- }
- static MyObject LoadObject(string file)
- {
- char buf[1000] ;
- ifstream i(file.c_str()) ;
- i>>buf ;
- MyObject *p=(MyObject*)buf ;
- i.close() ;
- return (*p);
- }
- MyObject ShowX() //一段最简单的代码
- {
- cout<<"x="<<x<<endl ;
- return (*this) ;
- }
- private :
- int x ;
- } ;
- int main()
- {
- MyObject<int> obj(443);
- MyObject<int>::StoreObject(&obj) ;
- MyObject<int>::LoadObject("c:\\obj.xxx").ShowX();
- return 0 ;
- }
#include <iostream>
#include <fstream>
#include <string>
using namespace std ;
template <class T>
class MyObject
{
public :
MyObject(T x)
{
cout<<"对象构造中!"<<endl ;
this->x=x ;
}
~MyObject()
{
}
static void StoreObject(MyObject *obj)
{
long length=sizeof(*obj) ;
ofstream *o=new ofstream("c:\\obj.xxx") ;
o->write((char*)obj,length) ;
o->close() ;
delete o ;
}
static MyObject LoadObject(string file)
{
char buf[1000] ;
ifstream i(file.c_str()) ;
i>>buf ;
MyObject *p=(MyObject*)buf ;
i.close() ;
return (*p);
}
MyObject ShowX() //一段最简单的代码
{
cout<<"x="<<x<<endl ;
return (*this) ;
}
private :
int x ;
} ;
int main()
{
MyObject<int> obj(443);
MyObject<int>::StoreObject(&obj) ;
MyObject<int>::LoadObject("c:\\obj.xxx").ShowX();
return 0 ;
}
紧接着问题由来了,的确上面的代码可以实现简单对象的序列化但是,,当类的成员是另一个类的实例的时候,那么又应该怎么弄呢? 再运行下面代码发现 ,程序在序列化到文件的时候崩溃了...什么原因呢 。。这根我们在java中和MFC对于成员对象序列化一样,在java中如果成员对象没有实现Serializable接口那么那么就会抛出一个异常 ,
在MFC中如果不成员对象所在的类如果不从CObject派生以及 增加一些宏定义那么也会程序崩溃。。看下面代码在上面代码修改后就出现了问题。程序就会崩溃。。。
对于类内部的成员对象如何进行进一步的一层一层的序列化呢?这个问题麻烦了..... 下次在继续 睡觉 。。。
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std ;
- class Data
- {
- public:
- void OutPut()
- {
- cout<<"输出成员对象!"<<endl ;
- }
- } ;
- template <class T>
- class MyObject
- {
- public :
- MyObject(T x)
- {
- cout<<"对象构造中!"<<endl ;
- this->x=x ;
- dt=new Data() ;
- }
- ~MyObject()
- {
- delete dt ;
- }
- static void StoreObject(MyObject *obj)
- {
- long length=sizeof(*obj) ;
- ofstream *o=new ofstream("c:\\obj.xxx") ;
- o->write((char*)obj,length) ;
- o->close() ;
- delete o ;
- }
- static MyObject LoadObject(string file)
- {
- char buf[1000] ;
- ifstream i(file.c_str()) ;
- i>>buf ;
- MyObject *p=(MyObject*)buf ;
- i.close() ;
- return (*p);
- }
- MyObject ShowX() //一段最简单的代码
- {
- cout<<"x="<<x<<endl ;
- return (*this) ;
- }
- private :
- int x ;
- Data *dt;
- } ;
- int main()
- {
- MyObject<int> obj(443);
- MyObject<int>::StoreObject(&obj) ;
- MyObject<int>::LoadObject("c:\\obj.xxx").ShowX();
- return 0 ;
- }