以C++为例子,通过操作内存模拟实现对象的持久化存储(一)
来源:互联网 发布:构架师和程序员 编辑:程序博客网 时间:2024/06/07 08:02
可以实现对象的存储到本地机器以及从文件加载对象而不用任何的库来辅助 、 下面开始 :
我的思路是内存,因为我们知道我们所有的数据都是存储在内存中的二进制数据 。 那么可以知道 我们的程序中的类的对象在运行过程中也相应的在他进城的存储
空间中对应一段二进制数据,那么我们是否可以将这个数据保存到本地然后从本地加载,我就兴奋的去尝试了,下 果然可以....原来对象序列化的原理是如此简单:
说道这里下面代码只完成了 简单的包含基本数据类型和方法的类进行序列化 ...经验证可以实现简单序列化 。。。
- #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 ;
- }
- 以C++为例子,通过操作内存模拟实现对象的持久化存储(一)
- 以C++为例子,通过操作内存模拟实现对象的持久化存储(一)
- C++通过操作内存模拟序列化---实现多种类型的序列化(2)
- 将对象转换为xml以持久化的圣器 xtream
- 对象持久化操作
- 以字节为单位显示变量的内存存储数据
- 以字节为单位显示变量的内存存储数据
- 以字节为单位显示变量的内存存储数据
- Hibernate持久化对象的级联操作
- TAO教程之四:改进服务端之通过POA策略实现持久化的对象引用
- React Native 之 通过AsyncStorage 实现数据持久化操作
- C#.Net 持久化对象为XML文件
- Rtti实现对象的XML持久化
- C++:通过一个点的数据对象转换成字符串的例子来说明整数转化为字符串
- 以C程序为例的内存分布详解
- 模拟可变分区存储管理的内存分配(C)
- Hibernate -- 操作持久化对象
- Hibernate操作持久化对象
- 取消office每次打开都需要安装!
- an software engineer should be
- 趣味谈UML类图
- 17-1.1 体会函数参数传递引用型排序
- Uva 10917 Walk Through the Forest
- 以C++为例子,通过操作内存模拟实现对象的持久化存储(一)
- 16 %>_<%
- Linux下程序(进程)如何获取自身的绝对路径
- matlab下拍照功能的实现
- 使用storyboard 传值
- 实现邮箱中选择所有邮箱功能
- UVA 10344
- 精通BitmapData
- tomcat for AndroidPN