
nico wrote:
Hello.
When we serialize a polymorphic object, an archive stores the information about true_type (i.e. the concrete type of an object) only if it is not equal to this_type (i.e. the type of the serialized object).
I.e. : the type of the serialized object is stored only if it is serialized from a base class.
How can I force the archive to always store the type?
replace ar & t; with ar & static_cast<base_type &>(t); Robert Ramey
PS : a word about why I'm trying to do that. When the true_type is "registered" (I'm not sure register is the right word), we can deserialize the object to any base class. Exemple:
SERIALIZATION: Leaf* leaf = new Leaf(); ar & leaf;
SERIALIZATION: Base* b = 0; ar & b; // would work only if Leaf has been saved in the archive as true_type
In general, you should be sure that that type "saved" is always the same as the type "loaded".
For tools in a multi-team project, it' powerfull : data for tests has been serialized by a team, and the other team can deserialize, let's say an image, to the interface class IImage without asking: what was the true_tyep
an Image3D, an AImage2D, an Image2DRGB, an Image2DRAW, and so on.
To get the most leverage from your design, you should make modules which use polymorphic types ONLY use the base class (reference or pointer) and avoid using the derived type at all. For example: instead of using derived_type d(initialization...) derived_type *dptr = new derived_type(initialization...) use base_type & b = d(initialization...) base_type *bptr = new derived_type(initialization...); Better yet use base_type &b = factory(initilization....) where factory returns some a base_type & etc. .... Robert Ramey
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost