Boost logo

Boost Users :

Subject: [Boost-users] Boost serialization through base class
From: Joe Drumm (joe.drumm_at_[hidden])
Date: 2010-10-13 13:20:29


Hi,

I've tried using the most basic example I can find in the
documentation to serialize a class using a pointer to a polymorphic
base. However, it only serializes the base class. I'm currently on
1.34.1.

Am I missing something obvious? Sample code below:

class base
{
public:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive& ar, const unsigned int)
        {
        }
};

class derived : public base
{
public:
        derived()
                : foo(5)
        {
        }

        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive& ar, const unsigned int)
        {
                boost::serialization::base_object<base>(*this);
                ar & BOOST_SERIALIZATION_NVP(foo);
        }

        int foo;
};

BOOST_CLASS_EXPORT_GUID(base, "base")
BOOST_CLASS_EXPORT_GUID(derived, "derived")

TEST(OMSTestSuite, BasicDeserializeTest)
{
        std::ofstream ofs("test.out");

        boost::archive::xml_oarchive toa(ofs);

        base *b = new derived;
        toa & BOOST_SERIALIZATION_NVP(b);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net