Serializing Private Class Members with Cereal

I want to serialize private member variables of my Player class using cereal. How can I accomplish this?

To serialize private member variables of your Player class using cereal, you can follow these steps:

Step 1: Include the necessary cereal headers:

#include <cereal/access.hpp>
#include <cereal/types/string.hpp>

Step 2: In your Player class, make the serialization functions private and befriend cereal::access:

class Player {
private:
  std::string name;
  int level;
  int health;

  friend class cereal::access;

  template<class Archive>
  void serialize(Archive& ar) {
    ar(name, level, health);
  }
};

By making the serialize function private and friending cereal::access, you grant cereal access to your class's private members for serialization purposes.

Step 3: Serialize and deserialize the Player object as usual:

#include <cereal/archives/binary.hpp>
#include <fstream>

int main() {
  // Serialization
  Player player("Alice", 10, 100);
  std::ofstream outputFile("player.dat");
  cereal::BinaryOutputArchive outputArchive(
    outputFile);
  outputArchive(player);

  // Deserialization
  Player deserializedPlayer;
  std::ifstream inputFile("player.dat");
  cereal::BinaryInputArchive inputArchive(
    inputFile);
  inputArchive(deserializedPlayer);
}

By following this approach, cereal will be able to access and serialize the private member variables of your Player class without the need to make them public or use getter/setter functions.

Note: Be cautious when serializing private members, as it can potentially break encapsulation. Only serialize data that is necessary for saving and loading the object's state.

A complete example is below:

#include <cereal/archives/binary.hpp>
#include <cereal/types/string.hpp>
#include <fstream>
#include <iostream>

class Player {
 public:
  Player() = default;
  Player(const std::string& name, int level,
    int health) : name(name), level(level),
    health(health) {}

  std::string GetName() { return name; }
  int GetLevel() { return level; }
  int GetHealth() { return health; }

 private:
  std::string name;
  int level;
  int health;

  friend class cereal::access;

  template <class Archive>
  void serialize(Archive& ar) {
    ar(name, level, health);
  }
};

int main() {
  // Serialization
  Player player("Alice", 10, 100);
  {
    std::ofstream outputFile("player.dat");
    cereal::BinaryOutputArchive outputArchive(
      outputFile);
    outputArchive(player);
  }

  // Deserialization
  Player deserializedPlayer;
  {
    std::ifstream inputFile("player.dat");
    cereal::BinaryInputArchive inputArchive(
      inputFile);
    inputArchive(deserializedPlayer);
  }

  std::cout << "Deserialized Player:\n";
  std::cout << "Name: "
    << deserializedPlayer.GetName() << "\n";
  std::cout << "Level: "
    << deserializedPlayer.GetLevel() << "\n";
  std::cout << "Health: "
    << deserializedPlayer.GetHealth() << "\n";
}
Deserialized Player:
Name: Alice
Level: 10
Health: 100

Binary Serialization using Cereal

A detailed and practical tutorial for binary serialization in modern C++ using the cereal library.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Serializing Vectors of Custom Types
How can I serialize a vector of my custom Player objects using cereal?
Serializing Maps of Polymorphic Objects
I have a std::map where the value type is a pointer to the base Character class, but it actually points to derived Monster objects. How can I serialize and deserialize this map correctly?
Versioning Serialized Game Save Files
I'm working on a game and want to ensure that save files from older versions can still be loaded in newer versions. How can I handle this using cereal's versioning features?
Serializing Non-Default-Constructible Classes with Cereal
I have a Weapon class that requires parameters in its constructor and doesn't have a default constructor. How can I serialize and deserialize objects of this class using cereal?
Serializing Enum Classes with Cereal
I have an enum class called State in my game. How can I serialize and deserialize variables of this enum class type using cereal?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant