利用boost的serialization库实现c++对象的序列化与反序列化

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

#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) { this->name = name; }
    private:
        friend class b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值