[Serialization] How to serialize a derived type but deserialize it as a base type pointer?

I know that we can serialize/deserialize a derived type through a base type pointer (using register_type()). But I cannot serialize a derived type and deserialize it through a base type pointer. It seems to me the data type in serialization and deserialization should be extactly the same one. What I want is, Base{...}; Derived : Base{...}; Derived d; boost::archive::binary_oarchive oa(...); oa << d; // serialize a derived object Base *b; boost::archive::binary_iarchive ia(...); ia.register_type(); ia >> b // Deseiralize to a base type pointer I have tried this but it does not work. Is there any way to make this work? Many thanks!!! -- View this message in context: http://boost.2283326.n4.nabble.com/Serialization-How-to-serialize-a-derived-... Sent from the Boost - Users mailing list archive at Nabble.com.

Jerry wrote:
I know that we can serialize/deserialize a derived type through a base type pointer (using register_type()). But I cannot serialize a derived type and deserialize it through a base type pointer. It seems to me the data type in serialization and deserialization should be extactly the same one.
when you serialize through a derived type, extra information about the derived type isn't saved as it's assumed it will be deserialized the same way.
What I want is,
Base{...}; Derived : Base{...};
//Derived d;
Base *d = new Derived; // use this instead !!
boost::archive::binary_oarchive oa(...); oa << d; // serialize a derived object
Base *b; boost::archive::binary_iarchive ia(...); ia.register_type(); ia >> b // Deseiralize to a base type pointer
I have tried this but it does not work. Is there any way to make this work?
Many thanks!!!

Thanks Robert. You mis-understood my intention. What we want is to serilaize an derived type object directly (not throught base type pointer) but deserialize it to a base type pointer. So we want the exactly the following codes: Derived d; oa << d; Base* b; ia >> b; But it seems this does not work. The reason we prefer this is that we have three nodes. One node sends/serialize the derived type object, while two other nodes are receivers. Howerver, one receives/deserializes the obj via derived type and the other deserializes the obj via base type pointer. We want, // The sender Derived d; oa << d; // One Receiver Base* b; ia >> b; // The other Receiver Derived dcopy; ia >> dcopy; On Tue, Apr 12, 2011 at 2:14 PM, Robert Ramey [via Boost] < ml-node+3443692-1803786549-229290@n4.nabble.com> wrote:
Jerry wrote:
I know that we can serialize/deserialize a derived type through a base type pointer (using register_type()). But I cannot serialize a derived type and deserialize it through a base type pointer. It seems to me the data type in serialization and deserialization should be extactly the same one.
when you serialize through a derived type, extra information about the derived type isn't saved as it's assumed it will be deserialized the same way.
What I want is,
Base{...}; Derived : Base{...};
//Derived d;
Base *d = new Derived; // use this instead !!
boost::archive::binary_oarchive oa(...); oa << d; // serialize a derived object
Base *b; boost::archive::binary_iarchive ia(...); ia.register_type(); ia >> b // Deseiralize to a base type pointer
I have tried this but it does not work. Is there any way to make this work?
Many thanks!!!
_______________________________________________ Boost-users mailing list [hidden email]http://user/SendEmail.jtp?type=node&node=3443692&i=0&by-user=t http://lists.boost.org/mailman/listinfo.cgi/boost-users
------------------------------ If you reply to this email, your message will be added to the discussion below:
http://boost.2283326.n4.nabble.com/Serialization-How-to-serialize-a-derived-... To unsubscribe from [Serialization] How to serialize a derived type but deserialize it as a base type pointer?, click herehttp://boost.2283326.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3440971&code=amVycnl1bnN3QGdtYWlsLmNvbXwzNDQwOTcxfC0xNjkzNTA5OTY4.
-- View this message in context: http://boost.2283326.n4.nabble.com/Serialization-How-to-serialize-a-derived-... Sent from the Boost - Users mailing list archive at Nabble.com.

Jerry wrote:
Thanks Robert. You mis-understood my intention. What we want is to serilaize an derived type object directly (not throught base type pointer) but deserialize it to a base type pointer. So we want the exactly the following codes:
I think Robert understands. The serialization library relies on the types saved to an archive be equal to the types being loaded from that archive.
Derived d; oa << d;
Requires: Derived d; ia >> d; If you do: Derived* d; // note pointer oa << (Base*)d The you can:
Base* b; ia >> b;
HTH, Jeff

On Mon, 11 Apr 2011 22:06:01 -0700 (PDT), Jerry wrote: Thanks Robert. You mis-understood my intention. What we want is to serilaize an derived type object directly (not throught base type pointer) but deserialize it to a base type pointer. So we want the exactly the following codes: Derived d; oa > b; But it seems this does not work. The reason we prefer this is that we have three nodes. One node sends/serialize the derived type object, while two other nodes are receivers. Howerver, one receives/deserializes the obj via derived type and the other deserializes the obj via base type pointer. We want, // The sender Derived d; oa > b; // The other Receiver Derived dcopy; ia >> dcopy; On Tue, Apr 12, 2011 at 2:14 PM, Robert Ramey [via Boost] wrote: Jerry wrote:
I know that we can serialize/deserialize a derived type through a base type pointer (using register_type()). But I cannot serialize a derived type and deserialize it through a base type pointer. It seems to me the data type in serialization and deserialization should be extactly the same one.
when you serialize through a derived type, extra information about the derived type isn't saved as it's assumed it will be deserialized the same way.
What I want is,
Base{...}; Derived : Base{...};
//Derived d; Base *d = new Derived; // use this instead !!
boost::archive::binary_oarchive oa(...);
oa Base *b;
boost::archive::binary_iarchive ia(...);
ia.register_type(); ia >> b // Deseiralize to a base type pointer
I have tried this but it does not work. Is there any way to make this work?
Many thanks!!!
_______________________________________________ Boost-users mailing list [hidden email] [1] http://lists.boost.org/mailman/listinfo.cgi/boost-users [2] ------------------------- If you reply to this email, your message will be added to the discussion below:http://boost.2283326.n4.nabble.com/Serialization-How-to-serialize-a-derived-... [3] ------------------------- View this message in context: Re: [Serialization] How to serialize a derived type but deserialize it as a base type pointer? [4] Sent from the Boost - Users mailing list archive [5] at Nabble.com. Links: ------ [1] http://user/SendEmail.jtp?type=node&node=3443692&i=0&by-user=t [2] http://lists.boost.org/mailman/listinfo.cgi/boost-users [3] http://boost.2283326.n4.nabble.com/Serialization-How-to-serialize-a-derived-... [4] http://boost.2283326.n4.nabble.com/Serialization-How-to-serialize-a-derived-... [5] http://boost.2283326.n4.nabble.com/Boost-Users-f2553780.html
participants (4)
-
boost@abracops.info
-
Jeff Flinn
-
Jerry
-
Robert Ramey