If you want to avoid re-developing the entire implementation of the serialization functions of a derived class, you can go in another direction: from the base to derived classes:
The virtual
class is provided in the base class to begin the serialization process. The client code calls this function with a pointer (or link). Also provide a virtual function that does serialization for the subclass. Call this function from the function of the base class Serialize
.
(EDIT) If you want to provide standard functions for serializing subclasses, but still want to be able to provide specialized functions for specific cases, then the function that serializes the subclasses should not be pure virtual. However, after reading my OP, it seemed to me that each subclass would be required to provide this functionality. To simulate this requirement, I made the DoSerialize
function pure virtual here.
Example:
class Base { public: void Serialize() const; virtual void DoSerialize() = 0; }; class Derived : public Base { public: void DoSerialize() { }; }; void Base::Serialize() const { this->DoSerialize();
source share