[Serialization]Why does serialization make use of two step initialization?
I'm using boost::serialization now and, indeed, it is very powerful, especially in type identification and object tracking, which make me very convenient. However, I think the lack of construction-time deserialization a pitfall. Usually the semantic of constructor is that either the constructed object is valid or an exception is thrown. However, boost::serialization require programmers provide a default constructor to construct an empty object and a load method (or serialize) to really initialize the object. Such a mechanism cannot prevent from invoking load method other than one time. What’s more, the default constructor usually lacks information to completely initialize the object, sometimes, it is even impossible to initialize. For instance, class B { public: void operator() (){…} } class A { private: B* pB; boost::thread m_theThread;//make use of pB->operator() as the entry point }; In the code above, pB in A cannot be correctly initialized in default constructor, but m_theThread MUST be initialized there. What entry point should we provide to m_theThread? I think serialization mechanism (especially deserialization part) should be provided for constructors. A tricky problem is how can constructor-time deserialization be coherent with that in “load” method and however can we eliminate duplicate code is another issue.
tankbattle wrote:
I'm using boost::serialization now and, indeed, it is very powerful, especially in type identification and object tracking, which make me very convenient. However, I think the lack of construction-time deserialization a pitfall.
the serialization library includes everything necessary to handle classes without default constructors. see the documentation boost/serialization/reference/serialization concept/pointers/Non-default constructors also see the test file test_non_default constructor. Robert Ramey
participants (2)
-
Robert Ramey
-
tankbattle