亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++ Boost Serialization庫超詳細(xì)獎金額

 更新時(shí)間:2022年12月02日 08:43:23   作者:無水先生  
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個(gè)可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱

一、說明

Boost.Serialization 也可以序列化指針和引用。因?yàn)橹羔槾鎯ο蟮牡刂?,所以序列化地址沒有多大意義。當(dāng)序列化指針和引用時(shí),被引用的對象被序列化。

二、示例代碼

示例 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)建一個(gè)類型為 animal 的新對象,并將其分配給指針 a。指針——不是*a——然后被序列化。 Boost.Serialization 自動序列化 a 引用的對象而不是對象的地址。

如果存檔已恢復(fù),則 a 不一定包含相同的地址。創(chuàng)建一個(gè)新對象并將其地址分配給 a。 Boost.Serialization 只保證對象與序列化的對象相同,而不保證其地址相同。

因?yàn)橹悄苤羔樑c動態(tài)分配的內(nèi)存結(jié)合使用,所以 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 進(jìn)行更新。 Boost.Serialization 當(dāng)前不支持來自 C++11 標(biāo)準(zhǔn)庫的智能指針,如 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)。與指針一樣,引用的對象會自動序列化。

到此這篇關(guān)于C++ Boost Serialization庫超詳細(xì)獎金額的文章就介紹到這了,更多相關(guān)C++ Serialization內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論