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, ...

Get Game Programming Using Qt now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.