網站首頁 編程語言 正文
一、說明
Boost.Serialization 也可以序列化指針和引用。因為指針存儲對象的地址,所以序列化地址沒有多大意義。當序列化指針和引用時,被引用的對象被序列化。
二、示例代碼
示例 64.8。序列化指針
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream>
std::stringstream ss;
class animal
{
public:
animal() = default;
animal(int legs) : legs_{legs} {}
int legs() const { return legs_; }
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; }
int legs_;
};
void save()
{
boost::archive::text_oarchive oa{ss};
animal *a = new animal{4};
oa << a;
std::cout << std::hex << a << '\n';
delete a;
}
void load()
{
boost::archive::text_iarchive ia{ss};
animal *a;
ia >> a;
std::cout << std::hex << a << '\n';
std::cout << std::dec << a->legs() << '\n';
delete a;
}
int main()
{
save();
load();
}
Example?64.8
示例 64.8 使用 new 創(chuàng)建一個類型為 animal 的新對象,并將其分配給指針 a。指針——不是*a——然后被序列化。 Boost.Serialization 自動序列化 a 引用的對象而不是對象的地址。
如果存檔已恢復,則 a 不一定包含相同的地址。創(chuàng)建一個新對象并將其地址分配給 a。 Boost.Serialization 只保證對象與序列化的對象相同,而不保證其地址相同。
因為智能指針與動態(tài)分配的內存結合使用,所以 Boost.Serialization 也提供了對它們的支持。
示例 64.9。序列化智能指針
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/scoped_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <iostream>
#include <sstream>
using namespace boost::archive;
std::stringstream ss;
class animal
{
public:
animal() = default;
animal(int legs) : legs_{legs} {}
int legs() const { return legs_; }
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; }
int legs_;
};
void save()
{
text_oarchive oa{ss};
boost::scoped_ptr<animal> a{new animal{4}};
oa << a;
}
void load()
{
text_iarchive ia{ss};
boost::scoped_ptr<animal> a;
ia >> a;
std::cout << a->legs() << '\n';
}
int main()
{
save();
load();
}
Example?64.9
示例 64.9 使用智能指針 boost::scoped_ptr 來管理動態(tài)分配的動物類型對象。包含頭文件 boost/serialization/scoped_ptr.hpp 以序列化此類指針。要序列化 ??boost::shared_ptr 類型的智能指針,請使用頭文件 boost/serialization/shared_ptr.hpp。
請注意,Boost.Serialization 尚未針對 C++11 進行更新。 Boost.Serialization 當前不支持來自 C++11 標準庫的智能指針,如 std::shared_ptr 和 std::unique_ptr。
示例 64.10。序列化引用
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <iostream>
#include <sstream>
using namespace boost::archive;
std::stringstream ss;
class animal
{
public:
animal() = default;
animal(int legs) : legs_{legs} {}
int legs() const { return legs_; }
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version) { ar & legs_; }
int legs_;
};
void save()
{
text_oarchive oa{ss};
animal a{4};
animal &r = a;
oa << r;
}
void load()
{
text_iarchive ia{ss};
animal a;
animal &r = a;
ia >> r;
std::cout << r.legs() << '\n';
}
int main()
{
save();
load();
}
Boost.Serialization 也可以毫無問題地序列化引用(參見示例 64.10)。與指針一樣,引用的對象會自動序列化。
原文鏈接:https://yamagota.blog.csdn.net/article/details/128111942
相關推薦
- 2022-07-29 Pytorch實現常用乘法算子TensorRT的示例代碼_python
- 2022-03-22 C++類的定義與實現_C 語言
- 2022-11-30 React中常見的TypeScript定義實戰(zhàn)教程_React
- 2022-04-15 windows+vscode穿越跳板機調試遠程代碼的圖文教程_python
- 2022-12-03 GCC?指令詳解及動態(tài)庫、靜態(tài)庫的使用方法_其它綜合
- 2022-12-26 一文帶你熟悉Go語言中函數的使用_Golang
- 2022-05-10 antd的timePicker.RangePicker設置結束時間不可早于開始時間
- 2023-01-14 python與matlab一些常用函數互轉問題_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支