Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2005-07-11 17:54:37


try making the base class a polymorphic class by adding the following changes:

virtual ~CMyCls1() = 0;
  "Yan Zhang" <Yan.Zhang_at_[hidden]> wrote in message news:F96935FCB9C3D549B3B6CD8915811A2F0FF00C7C_at_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_at_[hidden]
  http://lists.boost.org/mailman/listinfo.cgi/boost-users



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