Re: [Boost-users] How to serialize a derived class object withbaseclass pointer?

Hi Robert, I know that it works for abstract base class. But if the base class is not abstract class how you do it? Is it possible? Thanks, yan _____ From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Monday, July 11, 2005 3:55 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] How to serialize a derived class object withbaseclass pointer? try making the base class a polymorphic class by adding the following changes: virtual ~CMyCls1() = 0; "Yan Zhang" <Yan.Zhang@mitchell.com> wrote in message news:F96935FCB9C3D549B3B6CD8915811A2F0FF00C7C@mail60nt.mitchell.com... Hi, I'm trying to serialize an object of derived class with a base class pointer, somehow only base class's serialization member function got called. Here is the code (simplified): class CMyCls1 { public: CMyCls1() : m(10) {} ~CMyCls1() {} private: friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(m); } int m; virtual ~CMyCls1() = 0; }; class CMyCls2 : public CMyCls1 { public: CMyCls2() : x(1.5) {} ~CMyCls2() {} private: friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(CMyCls1); ar & BOOST_SERIALIZATION_NVP(x); } double x; virtual ~CMyCls2(); }; void main () { CMyCls1 *pmycls = new CMyCls2; ... oa.register_type(static_cast<CMyCls1 *>(NULL)); oa.register_type(static_cast<CMyCls2 *>(NULL)); oa << BOOST_SERIALIZATION_NVP(pmycls); ... } Look at the archive file, only base class data is serialized. Actuall, CMyCls2::serialization() is not called at all. How do you serialize a derived class object with a pointer of base class? Thanks, Yan _____ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

First - there's no way to do it. Second - that's probably the best anyway. This better mirrors standard C++ behavior in that non-polymophic (mono-morphic?) base classes do not have any access to drived data or functions. Third - there really is a a way to do it using one's own extended_type_info strategy - see docs and test_no_rtti. But I would only recommend this for those how can't use an rtti system and who do thier own dental work. Robert Ramey "Yan Zhang" <Yan.Zhang@mitchell.com> wrote in message news:F96935FCB9C3D549B3B6CD8915811A2F0FF00D8F@mail60nt.mitchell.com... Hi Robert, I know that it works for abstract base class. But if the base class is not abstract class how you do it? Is it possible? Thanks, yan ------------------------------------------------------------------------------ From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Monday, July 11, 2005 3:55 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] How to serialize a derived class object withbaseclass pointer? try making the base class a polymorphic class by adding the following changes: virtual ~CMyCls1() = 0; "Yan Zhang" <Yan.Zhang@mitchell.com> wrote in message news:F96935FCB9C3D549B3B6CD8915811A2F0FF00C7C@mail60nt.mitchell.com... Hi, I'm trying to serialize an object of derived class with a base class pointer, somehow only base class's serialization member function got called. Here is the code (simplified): class CMyCls1 { public: CMyCls1() : m(10) {} ~CMyCls1() {} private: friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(m); } int m; virtual ~CMyCls1() = 0; }; class CMyCls2 : public CMyCls1 { public: CMyCls2() : x(1.5) {} ~CMyCls2() {} private: friend class boost::serialization::access; template<class Archive> void serialize(Archive &ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(CMyCls1); ar & BOOST_SERIALIZATION_NVP(x); } double x; virtual ~CMyCls2(); }; void main () { CMyCls1 *pmycls = new CMyCls2; . oa.register_type(static_cast<CMyCls1 *>(NULL)); oa.register_type(static_cast<CMyCls2 *>(NULL)); oa << BOOST_SERIALIZATION_NVP(pmycls); . } Look at the archive file, only base class data is serialized. Actuall, CMyCls2::serialization() is not called at all. How do you serialize a derived class object with a pointer of base class? Thanks, Yan ---------------------------------------------------------------------------- _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Yan Zhang wrote:
Hi Robert,
I know that it works for abstract base class. But if the base class is not abstract class how you do it? Is it possible?
Thanks,
You do not have to make the function pure virtual, just make the destructor virtual. That should solve your problem. Orhun Birsoy
participants (3)
-
Orhun Birsoy
-
Robert Ramey
-
Yan Zhang