I perform serialization / deserialization of a subclass object by a pointer to its base class. Everything works Ok, yet I miss one feature: adding a runtime parameter to the constructor of the object being deserialized, example:
class Base {
public:
Base(AnotherClass* another)
:m_another(another)
{}
protected:
AnotherClass* m_another;
};
class Derived : public Base {
public:
Derived(AnotherClass* another)
:Base(another)
{}
Derived()
:Base(nullptr)
{}
private:
/* different other members */
};
BOOST_CLASS_EXPORT(Derived);
...
My normal way to create a Derived object is:
Base* obj = new Derived(anotherObj);
The deserialization goes like this:
Base* obj;
ar >> obj;
The default constructor will be called (Derived()), and deserialization proceeds, BUT m_another is not deserialized, it should be passed to the constructor, all the other fields are deserialized.
Moreover, I cannot set m_another after the deserialization, because it actually influences the deserialization.
I can pass reference to anotherObj via a global variable - ugly, but works.
Is there any way to solve it in a not so ugly manner ?