那位大侠可以给出仿真MFC的Sericalize的源代码吗?

anglely168 2002-08-12 03:20:19
小弟在看<<深入浅出MFC>>的第三章的时候,看到了侯捷关于前几个技术的仿真,唯独缺了
这个技术的仿真,于是自己来写代码测试,没想到一写下出,发现N多那难题,特别是对CArchive类的仿真,我写了半天,一大堆调用关系搞得我的头大了,CArchive 调用CRuntimeClass,又要调用pObject。。。。,却没看见它和文件是怎么打交道的?没看到文件流(fstream),有谁对这个问题深入研究过,奉献上代码,高分伺候!!
...全文
27 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈硕 2002-09-09
  • 打赏
  • 举报
回复
gz...
anni 2002-09-09
  • 打赏
  • 举报
回复
给出email,我这有代码
anglely168 2002-08-13
  • 打赏
  • 举报
回复
to anrxhzh(百宝箱) :
你说的STL和设计模式方面我现在正在用,由于我是在linux下编程,MFC的好多东西不能用,只能自己来仿真!这个问题已经缠了我好几天了!还请多指教!!你有这方面的源码吗?若有给我一份!!

my_email:fans86@163.net
anrxhzh 2002-08-13
  • 打赏
  • 举报
回复
anglely168(我不是天使!) :

那篇文章中的代码是用来阐述实现技术的伪码,并不是可以直接拿来编译的代码。说实话,要理解这些伪码是需要一些基本功的,最好有STL和设计模式方面的知识。
anglely168 2002-08-12
  • 打赏
  • 举报
回复
我把一遍资料上的文章抄录如下:(编译提示很多地方有错大家帮忙来该错)
class PObject {
public:
PObject (void) {}
virtual ~PObject (void) {}
virtual const char *get_class_name (void) const = 0;
virtual int read (ifstream &in) = 0;
virtual int write (ofstream &out) const = 0;
virtual PObject *create (void) const = 0;
};

// Search structure that manages a set of PObjects
class Collection {
public:
int insert (PObject *);
int remove (PObject *);
// ...
};

// Library for reading/writing Pobjects
class IOLib {
public:
enum { MAX_CLASS_NAME = 256 };
IOLib (void) {}
int write (const char *filename);
int read (const char *filename);
void add_object (PObject*);
void remove_object (PObject*);
private:
// There a prototype for each class
Collection prototypes;
// The actual objects
Collection objects;
};

int IOLib::write (const char *filename)
{
ofstream out (filename);
if (out) {
Iterator iter (objects);
// iterate through all the objects in the objects
// collection for each one write its name and
// then use its write() method.

for (objp = iter.first (); objp != 0; objp = iter.next ()) {
out.write (objp->get_class_name (),
strlen (objp->get_class_name ()) + 1);
// write the object binary representation
// using its write () method
if (objp->write (out)==0)
return 0;
}
}
return 1;
}

// Reads a file with object returns TRUE on success
// and FALSE otherwise
int IOLib::read (const char *filename)
{
ifstream in (filename);
while (in) {
// buffer is big enough for class name.
char buf[MAX_CLASS_NAME];

// Reads class name up to terminating '\0'.
for (int i=0; in.get (buf[i]) && buf[i]; i++)
continue;

// Find a prototype and create a new instance
// (error checking is omitted).
PObject *prototype = objects.find (buf);
PObject *objp = prototype->create ();

// Read in from the file.
if (objp->read (in) == 0)
return 0;
}
return (in.eof ());
}

class Note : public PObject {
public:
Note (const char *str) { strcpy (buf, str); }
const char *get_class_name (void) const {
return "Note";
}
int read (ifstream &in) {
in.read (&buf, sizeof (buf)); return in.good ();
}
virtual int write (ofstream &out) const {
out.write (&buf, sizeof (buf)); return .out.good ();
}
virtual PObject *create (void) const {
return new Note ("");
}

private:
enum { BUFLEN = 128 };
char buf[BUFLEN];
};
anglely168 2002-08-12
  • 打赏
  • 举报
回复
to anrxhzh(百宝箱):
奇怪,http://www.stat.cmu.edu/~lamj/sigs/c++-report/cppr9605.f.cohen.html里面的例子我总么编译通不过?
我用的是VC6.0
e2wugui 2002-08-12
  • 打赏
  • 举报
回复
大问题。

anrxhzh 2002-08-12
  • 打赏
  • 举报
回复
Object Persistence是OO中的一道难题,探讨起来足够写一大摞专著的。面对这个难题,我们首先要搞清楚做什么,然后才是怎么做。

做什么的问题,我建议你看下面的文章,它把概念讲得非常清晰:
Object Persistence Through Serialization
Ray Walker
Software Engineer
Candle Corporation
http://www.candle.com/www1/vgn/images/portal/cit_1749/43198ccr_vol20no6.pdf

怎么做的问题,我建议你看下面的文章,它把各种实现技术娓娓道来:
Lightweight Persistence in C++
Shimon Cohen
http://www.stat.cmu.edu/~lamj/sigs/c++-report/cppr9605.f.cohen.html

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧