[原]mfc大观之四(串行保存)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163 com】 在mfc当中,保存其实是一件很简单的事情。你要做

【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】

在mfc当中,保存其实是一件很简单的事情。你要做的很简单,第一步就是编写一个serial的虚函数,另外一步就是CArchive调用解决,整个过程非常简单和流畅。当然,为了判断class之间是否存在集成关系,它和之前的类一样也使用了runclass的方法,剩下的就是一些算术符号重载的trick了。大家有兴趣参考一下下面的代码,就明白其中的逻辑了。

#include <iostream>using namespace std;struct runclass { char name[64]; struct runclass* next;};class cwrite {public: cwrite() { cout << "cwrite::cwrite()" << endl; } ~cwrite() { cout << "cwrite::~cwrite()" << endl; }};#define CREATE_SERIAL(basic) / static runclass run_##basic; / static runclass* get_run_class() {return &run_##basic;} / friend cwrite& operator <<(cwrite& c, basic& b) {b.serial(c); return c;}class cobject {public: cobject() { cout << "cobject::cobject()" << endl; } virtual ~cobject() { cout << "cobject::~cobject()" << endl; } virtual void serial(cwrite& c) { cout << "cobject::serial()" << endl; }public: CREATE_SERIAL(cobject)};runclass cobject::run_cobject = {"cobject", NULL};#define IMPLEMENT_SERIAL(basic, origin) / runclass basic::run_##basic = {#basic, &origin::run_##origin}class cwnd : public cobject {public: cwnd(){ cout << "cwnd::cwnd()" << endl; } ~cwnd() { cout << "cwnd::~cwnd()" << endl; } void serial(cwrite& c) { cout << "cwnd::serial()" << endl; }public: CREATE_SERIAL(cwnd)};IMPLEMENT_SERIAL(cwnd, cobject);boolisKindOf(runclass* basic, runclass* origin){ if(NULL == basic || NULL == origin) return false; while(basic->next) basic = basic->next; return 0 == strcmp(basic->name, origin->name) ? 1:0;}intmain(int argc, char* argv[]){ cwrite c; cobject obj; cwnd wnd; c << obj; c << wnd; return 0;}
未登录用户
全部评论0
到底啦