欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ Boost Serialization庫(kù)超詳細(xì)獎(jiǎng)金額

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

一、說(shuō)明

Boost.Serialization 也可以序列化指針和引用。因?yàn)橹羔槾鎯?chǔ)對(duì)象的地址,所以序列化地址沒(méi)有多大意義。當(dāng)序列化指針和引用時(shí),被引用的對(duì)象被序列化。

二、示例代碼

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

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

因?yàn)橹悄苤羔樑c動(dòng)態(tài)分配的內(nèi)存結(jié)合使用,所以 Boost.Serialization 也提供了對(duì)它們的支持。

示例 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 來(lái)管理動(dòng)態(tài)分配的動(dòng)物類(lèi)型對(duì)象。包含頭文件 boost/serialization/scoped_ptr.hpp 以序列化此類(lèi)指針。要序列化 ??boost::shared_ptr 類(lèi)型的智能指針,請(qǐng)使用頭文件 boost/serialization/shared_ptr.hpp。

請(qǐng)注意,Boost.Serialization 尚未針對(duì) C++11 進(jìn)行更新。 Boost.Serialization 當(dāng)前不支持來(lái)自 C++11 標(biāo)準(zhǔn)庫(kù)的智能指針,如 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 也可以毫無(wú)問(wèn)題地序列化引用(參見(jiàn)示例 64.10)。與指針一樣,引用的對(duì)象會(huì)自動(dòng)序列化。

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

相關(guān)文章

最新評(píng)論