The general technique is to define a function named
"serialize" in the ccs::serialization
namespace, and that function must take a "serializer object" that can be
of any type, and a reference to the class that is to be serialized. The
function then passes to the "serializer object" the members of the class
instance that must be serialized. For example, a banking program manages
instances of this bank_account class:
class bank_account {
public:
bank_account(int id, float balance) : id_(id), balance_(balance) {}
int id_; // this is a PUBLIC member
float balance_; // this is a PUBLIC member
};The following code serializes instances of the bank_account class:
namespace ccs {
namespace serialization {
template <typename S> void serialize(S& s, bank_account& ba) {
s & ba.id_ & ba.balance_;
}
}
}Notice that because the function is outside the class, the members
must be declared "public:" otherwise the serialization
function could not write to the members, which would make loading
impossible. Also, the constructor of the bank account class must be
default-constructible, otherwise it will not be possible to load a
vector, say, of bank_account instances.
Thus:
class bank_account {
public:
bank_account(int id = 0, float balance = 0) :
id_(id), balance_(balance) {}
...
};