代码示例及讲解
#include <cstddef>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/string.hpp>//本例中需要对string进行存储,所以引入string.hpp;此外还有一些预定义的模板,如vector、list、map等
#include <boost/serialization/access.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Cat{
public:
Cat() {}//每个对象都必须提供一个默认构造函数
Cat(string name) { this->name = name; }
void print() { cout<<"my name is "<<this->name<<endl; }
string getName() { return this->name; }
void setName(string name) {

本文介绍了如何利用Boost库的serialization模块实现C++对象的序列化和反序列化操作。通过示例展示了如何对自定义类`Cat`及其指针类型`CatPtr`进行序列化和反序列化,并提供了保存和恢复对象的通用函数。代码中详细解释了关键步骤,包括友元类、序列化模板函数和文件操作。

1566

被折叠的 条评论
为什么被折叠?



