I am developing a simple data to XML serializer for training purposes. Now that I have my base class of a serializer done, I want to derive a specialized serializer for the data structures of different projects. My base serializer has functions like this:
class Serializer {
public:
template <class T> void add_value(const std::string &key, T *value);
template <class T> void add_value(const std::string &key, std::vector<T*> *value);
};
The base serializer is able to handle most primitive data structures (int, double, bool, etc.) and there are some overloads for a few more complex structures (vectors, maps, etc.). Lets assume I now have a project with the following data structure:
struct MyStruct {
int width, height;
};
I would now like to derive a serializer from my base serializer that is able to handle that structure, as well as the structures it already knows:
class MyStructSerializer : public Serializer {
void add_value(const std::string &key, MyStruct *value);
};
Problem is: After overloading the add_value function in the child class, the parent class forms of it are no longer available. Is it possible to overload the add_value function as a specialization to the functions provided in the parent class, i.e. keep the child class from forgetting the more general method of its parent after overloading?
The
usingkeyword will import members of the base class into the derived class:Although, wouldn’t it be easier to specialize
Serializer::add_value, rather than derive fromSerializer? Like this: