2

In the boost tutorial and example of using shared pointers, they have a class A, and they create a shared pointer pointing to an object of class A:

boost::shared_ptr<A> spa(new A);

Then they serialize it:

std::ofstream ofs(filename.c_str());
boost::archive::text_oarchive oa(ofs);
oa << spa;

So why does the class A have to have this function?

void serialize(Archive & ar, const unsigned int /* file_version */); 

The reason I want to use a shared pointer is to avoid defining this function for some of my complex classes. Currently I'm not using a shared pointer, I'm using a real pointer, and I'm actually serializing the address the pointer is pointing at.

Boris Dalstein
  • 7,015
  • 4
  • 30
  • 59
yonigo
  • 987
  • 1
  • 15
  • 30
  • How else would the serialization library know, how to serialize an object of type `A`? Somebody needs to tell it how to do it and this is (one of the) way(s) Boost.Serialization deals with this. – Paul Michalik Jul 17 '13 at 17:38
  • but i dont want to serialize A, i want to serialize the address.. – yonigo Jul 17 '13 at 17:53
  • +1 This is a useful question – Boris Dalstein Jul 17 '13 at 20:04
  • @yonigo I probably still don't understand, but for that you don't need a sophisticated serialization library. How is it useful to serialize the value of a pointer (an address)? – Paul Michalik Jul 18 '13 at 17:29

2 Answers2

0

The reason i want to use a shared_pointer is to avoid defining this function for some of my complex classes.

In my opinion, this is a wrong reason to use shared pointers. Using shared pointers is good, but the reason is that it does memory management for you, so that you do not have to call delete yourself. In some (rare) cases, using smart pointers may not be the best solution and you would prefer to do memory management yourself. This is the most important criteria to decide to use smart pointers or not, serialization being a side benefit/trouble comes after.

Anyway:

I am not an expert in the boost::archive framework, but it seems serializing to the class Archive is more subtle than just printing the value, since it also uses a "version" in case you want to change how you serialize your object, but ensure retro-compatibility with files written with the older version. That is why only using the operator<< of your object (in your case, either a A* or a shared_ptr<A>) is not enough. You do need the method void serialize(...).

However, boost provides a convenient macro to automatically define this method for shared_ptr<A> objects:

BOOST_SERIALIZATION_SHARED_PTR(A)

Also, I guess the reason it works automagically with A* objects is that there is a default implementation for these.

Boris Dalstein
  • 7,015
  • 4
  • 30
  • 59
0

There is a good answer to this in Boost documentation.

Here is the example they gave:

template<class Archive, class T>
inline void save(
    Archive & ar,
    const boost::shared_ptr<T> &t,
    const unsigned int /* file_version */
){
    const T * t_ptr = t.get();
    // just serialize the underlying raw pointer
    ar <<: boost::serialization::make_nvp("px", t_ptr);
}

template<class Archive, class T>
inline void load(
    Archive & ar,
    boost::shared_ptr<T> &t,
    const unsigned int file_version
){
    T* r;
    // recover the underlying raw pointer
    ar >> boost::serialization::make_nvp("px", r);

    // To Do - match up with other shared pointers which 
    // use this same raw pointer.
    ...
}

The full answer is in the Boost docs.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • 1
    This is a little better than your previous two attempts, but there is still a problem here; Now all you've done seems to be copied-and-pasted from the docs, rather than giving an answer in your own words. Using quotes and links is OK, but neither should be a large part of your answer - or even be required at all for your answer to make sense. – Andrew Barber Jul 18 '13 at 15:10