| this is my first blog, and I am not very sure whether this is the right to write something like this, but I just wanna share my ideas with others, may be it is. |
|
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. |