生成可串行化的类:
如果要用CArchive来保存一个对象,那么这个对象的类必须支持串行化。
要生成一个可串行化的类,需要下面5个步骤:(以生成第11讲中用过的CGraph类为例)
1 . Deriving your class from CObject (or from some class derived from CObject).
从CObject派生你的类
class CGraph :public CObject
2 . Overriding the Serialize member function.
覆盖Serialize成员函数
void Serialize( CArchive& ar);//重载函数
3 . Using the DECLARE_SERIAL macro in the class declaration.
声明类的时候调用DECLARE_SERIAL宏
DECLARE_SERIAL( CGraph )
4 . Defining a constructor that takes no arguments.
定义一个不带参数的构造函数
CGraph();
5 . Using the IMPLEMENT_SERIAL macro in the implementation file for your class.
在实现文件中使用IMPLEMENT_SERIAL宏
IMPLEMENT_SERIAL(CGraph,CObject,1), //1为版本号,
从文件中读取数据时,会判断一下保存的数据版本号与当前类的版本号是否一致;
TRUE则加载数据成功,FALSE加载数据失败。