My Understanding of MFC serialization

来源:互联网 发布:清除上网痕迹软件 编辑:程序博客网 时间:2024/04/29 16:09

First, I am Chinese, but I'd like to write in English, if that really disgut you a lot, please choose to ignore me. 

and those who can bear with my sucking English, read on.

Serialization

every object that is serializable must be dynamic creatable first,
in order to be dynamic creatable, an object or the objects of the same class
must have some runtime type information associated with it.
the runtime type information is represented by an instance of the CRuntimeClass in MFC.

DECLARE_DYNAMIC
DECLARE_DYNCREATE
DELCARE_SERIAL

IMPLEMENT_DYNAMIC
IMPLEMENT_DYNCREATE
IMPLEMENT_SERIAL

these sex macros are used associate an instance of CRuntimeClass with the class and
connect the runtime type information of one class with the MFC runtime type maps.

following is a brief overview of the steps in serializing an object.


an object, of used defined types, can be saved in
two ways, either by call its serialize member function, or call the insertion operator.
there is a insertion operator is defined for objects of CObject, since all serializable objects, except
primative types and CStirng, are derived from CObject whether directly or indirectly.
so serializable objects are CObject objects, the operator function will first save the CRuntime Object
associated with the class that the object belongs to. and then call the object's overrided serialize
member function. the key point is that every object takes care of itself(from MSDN).

the macros will create an overloaded extraction operator for the class, but that is just for verification.
and it is optional.

when to use operator , when to use serialize funciton?
the answer is it depends.

the general guidline is that use serialize when you know the type of the object being serialized,
I mean its exactly type, so if CDerived is derived from CBase, a CDerived object is a CBase object, but
they are different here.


use the operator when there are polymorphism.


the last rule is that, if you use serialze to save an object, you must also use serialize to load it.

any body wanna read this kind of stuff pleae go to http://www.aeonity.com/JumboGeng, my blog,

and it is more up to date.