I have a little problem with boost serialization. There are many examples that shows how to serialize a derived class pointer through the base class pointer by simply using BOOST_CLASS_EXPORT and BOOST_CLASS_EXPORT_IMPLEMENT. This is working fine and have no problems at all.
However, I do not want to serialize a pointer, as the deserialization in the other side should be again over a pointer and then, boost creates a new instance of the serialized object.
I can serialize a dereferenced pointer and then deserialize again over an existing object instance without problems, and no new instances are created. However, when the dereferenced pointer is over the base class, the derived class is not serialized as expected when serializing over pointers.
Working example:
Class A;
Class B : public A;
A* baseClass = new B();
ar << baseClass // works perfectly
Not working example:
Class A;
Class B : public A;
A* baseClass = new B();
ar << *baseClass; // only A is serialized
I can get it working by simple serializing over the derived class like:
B* derivedClass = new B();
ar << *derivedClass; // works fine
But all the references I have in my structures are of base class type. Also I cannot serialize the pointer as I do not need to instantiate new objetcs when deserializing, only "overwrite" the contents over an existing instance.
I have tried to serialize the pointer and trying to deserialize over an existing instance, but this does not work correctly. When I say deserialize over an existing instance, I mean:
A* baseClass = new B();
// baseClass is used in the program and in a given moment, its contents must be overwrite, so:
ar >> *baseClass;
As I said, I do not need a new instance of baseClass when deserializing. So, is there any way to get this working?