Boost logo

Boost Users :

From: Ivan Rachev (ivanr_at_[hidden])
Date: 2005-07-10 11:49:08


Hi Martin,

If class A from the example is replaced by char, how should this
situation be handled? Code follows at the end.

Thanks,
Ivan

template <typename T>
struct DynamicArray
{
     int Size;
     T* Element;

     friend class boost::serialization::access;
     template<class Archive>
     void save(Archive & ar, const unsigned int version) const
     {
       ar & Size;
       for (int i = 0; i < Size; ++i)
         ar & Element[i];
     }
     template<class Archive>
     void load(Archive & ar, const unsigned int version)
     {
       ar & Size;
       assert(Size >=0);
       if (Size > 0)
         {
         Element = new T[Size];
         for (int i = 0; i < Size; ++i)
           ar & Element[i];
         }
     }
     BOOST_SERIALIZATION_SPLIT_MEMBER()
};

int main(int argc, char* argv[])
{
     DynamicArray<char> ArrayOfAs, ArrayOfAs2;
     ArrayOfAs.Element = new char[5];
     ArrayOfAs.Size = 5;
     char* secondElement = &ArrayOfAs.Element[1];
     char* secondElement2;

     std::stringstream stream;
     {
      boost::archive::text_oarchive oa(stream);
      oa & ArrayOfAs;
      oa & secondElement;

     }

     {
     boost::archive::text_iarchive ia(stream);
     ia & ArrayOfAs2;
     ia & secondElement2;

     }

     assert(secondElement2 == &ArrayOfAs2.Element[1]);

    delete [] ArrayOfAs.Element;
    delete [] ArrayOfAs2.Element;
}


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