Time for action – serialization of a custom structure
Let's perform another small exercise by implementing functions that are required to use QDataStream
to serialize the same simple structure that contains the player information that we used for text streaming:
struct Player { QString name; qint64 experience; QPoint position; char direction; };
For this, two functions need to be implemented, both returning a QDataStream
reference that was taken earlier as an argument to the call. Apart from the stream itself, the serialization operator accepts a constant reference to the class that is being saved. The most simple implementation just streams each member into the stream and returns the stream afterwards:
QDataStream& operator<<(QDataStream &stream, const Player &p) { stream << p.name; stream << p.experience; stream << p.position; stream << p.direction; return stream; }
Complementary to this, deserializing is done by implementing...