I have a Message class which other classes inherit from. The inherited classes will need to have a serialisation member function. I would like to make these member functions virtual (so that I take advantage of dynamic dispatch).
class Message
{
private:
int x;
public:
virtualvoid serialise(char* str)
{
// do serialisation
}
};
class MessageWStr : public Message
{
private:
string str;
public:
virtualvoid serialise(char* str) override
{
// do serialisation
}
};
The problem arises with the serialisation library which I'm using which requires the class (being serialised) to have a template member function of the form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// in Message
template <typename T>
void serialize(T& t)
{
t.saveInt(x); // serialisation library function - serialises member variable x
}
// in MessageWStr
template <typename T>
void serialize(T& t)
{
t.saveStr(str); // serialisation library function - serialises member variable str
}
However, I cannot make these functions virtual as apparently c++ does not permit virtual template member functions.
I wondered if anyone might know of a solution to this problem?
And then to arrange to call this instead of f, which can essentially be done by implementing a less-general version of the visitor pattern and making this function into the visitor.
A second way is to exploit the commonality between every T that is passed to serialize. An example of that commonality could be that every T has a member function whose signature is void saveInt(int).
Maybe look up "visitor pattern" and "c++ type erasure".