serialization - Boost c++ serializing a char * -
i have class , trying serialize shared_ptr normal method of serializing object not working:
class object { public: object(); ~object(); shared_ptr<char>objectone; friend class boost::serialization::access; template <typename archive> void serialize(archive &ar, const unsigned int version) { ar & objectone; } };
i've attempted in way still doesn't work:
void serialize(archive &ar, const unsigned int version) { (int = 0; < (strlen(objectone.get())); i++) ar & objectone.get()[i]; }
any ideas how approach this? thanks.
some information:
i've included both shared_ptr header files:
#include <boost/serialization/shared_ptr.hpp> #include <boost/serialization/shared_ptr_132.hpp>
i've attempted convert string , serialize in way produces following error: boost::archive::archive_exception' what(): stream error
friend class boost::serialization::access; template <typename archive> void serialize(archive &ar, const unsigned int version) { if (objectone.get()) { string temp(objectone.get()); ar & temp; } ar & objectone; }
you missing key concepts. don't need share_ptr anymore if use std::string.
you can
class object { public: object(); ~object(); std::string name; template <typename archive> void serialize(archive &ar, const unsigned int version) { ar & name; } };
and done.
addition
given requirements have do
class test { public: friend class boost::serialization::access; test() { } template<class archive> void save(archive & ar, const unsigned int version) const { int len = strlen(data.get()); ar & len; (int index = 0; index < len; ++index) ar & data[index]; } template<class archive> void load(archive & ar, const unsigned int version) { int len = 0; ar & len; data = boost::shared_array<char>(new char[len+1]); (int index = 0; index < len; ++index) ar & data[index]; data[len] = 0; } boost_serialization_split_member() boost::shared_array<char> data; };
Comments
Post a Comment