Griffin
Griffin

Reputation: 2517

How to override what happens when boost::serialize gets a POINTER to an object

Hey so I understand boost serializes pointers automatically as long as you've defined the serialization function for the object it's pointing to, but

what do I do if I want to write a boost serialization function that takes a myClass pointer?

I don't want the boost to do the default action of saving the object that's pointed to and then restoring the pointer to point to that. I want the boost to do something different.

Upvotes: 1

Views: 446

Answers (1)

Philipp
Philipp

Reputation: 11833

If you need a behaviour that is different from the normal pointer serialization for your special class, you have two options:

Maybe you can adjust the serialize() methods of all structs/classes that contain your myClass pointer in order to achieve the behaviour that you want. However, if you have many such pointers around, this won't be an option. Another possibility might be to use a free function as described in http://www.boost.org/doc/libs/1_47_0/libs/serialization/doc/index.html (written for the myClass pointer).

The other option only works if you are using no more than one archive type (e.g. the binary archive). You can derive from the archive classes and add an overload for the method save (and load, respectively).

Upvotes: 1

Related Questions