2017-05-28 138 views
0

我是C++的新手,我無法找到如何序列化二進制對象的詳細示例,因此我可以將其保存到mysql服務器,然後進行反序列化。在C++中序列化和反序列化對象

物體看起來是這樣的:

class house{ 
     public: 
      int houseNumber; 
      bool isEmpty; 
      Dweller *dweller; 
}; 
    class Dweller{ 
     public: 
     int phoneNumber; 
     bool man; 
     Backpack backpack; 
}; 
    class Backpack{ 
     public: 
     int item1; 
     int item2; 
     int item3; 
     int item4; 
     float weight; 
     bool empty; 
}; 

我必須序列房屋目標,該公司擁有的其他對象。

+0

任何一種二進制格式? protobuf是一個選項嗎? https://developers.google.com/protocol-buffers/docs/overview – Anton

+0

我認爲protobuf將是一個選項 –

回答

0

我可以使用來解決問題:

#include <boost/serialization/serialization.hpp> 
#include <boost/serialization/vector.hpp> 

見我用#include "boost/serialization/vector.hpp"因爲現在的「居民」是一個載體,使事情變得更容易

然後代碼呆在這樣的:

class house{ 
    public: 
     int houseNumber; 
     bool isEmpty; 
     std::vector<Dweller> dweller; 
    private: 
     friend class boost::serialization::access; 
     template <class archive> 
     void serialize(archive &ar, const unsigned int version) 
     { 
     ar &houseNumber; 
     ar &isEmpty; 
     ar &dweller; 
     } 
}; 
class Dweller{ 
    public: 
     int phoneNumber; 
     bool man; 
     Backpack backpack; 
    private: 
     friend class boost::serialization::access; 
     template <class archive> 
     void serialize(archive &ar, const unsigned int version) 
     { 
     ar &phoneNumber; 
     ar &man; 
     ar &backpack; 
     } 
}; 
class Backpack{ 
    public: 
     int item1; 
     int item2; 
     int item3; 
     int item4; 
     float weight; 
     bool empty; 
    private: 
     friend class boost::serialization::access; 
     template <class archive> 
     void serialize(archive &ar, const unsigned int version) 
     { 
     ar &item1; 
     ar &item2; 
     ar &item3; 
     ar &item4; 
     ar &weight; 
     ar &empty; 
     } 
}; 

所以連載我用:

#include <iostream> 
#include <boost/archive/binary_oarchive.hpp> 
#include <boost/archive/binary_iarchive.hpp> 
#include <boost/serialization/serialization.hpp> 
#include <sstream> 

int main() 
{ 
    Backpack _backpack; 
    _backpack.item1 = 0; 
    _backpack.item1 = 1; 
    _backpack.item1 = 2; 
    _backpack.item1 = 3; 
    _backpack.item1 = 4; 
    _backpack.weight = 3.21; 
    _backpack.empty = false; 
    Dweller _dweller1; 
    _dweller.phoneNumber = 1234567; 
    _dweller.man = false; 
    _dweller.backpack = _backpack; 
    Dweller _dweller2; 
    _dweller.phoneNumber = 7654321; 
    _dweller.man = true; 
    _dweller.backpack = _backpack; 

    std::vector<Dweller> _dwellers; 
    _dwellers.push_back(_dweller1); 
    _dwellers.push_back(_dweller2); 
    house _house; 
    _house.houseNumber = 4; 
    _house.isEmpty = false; 
    _house.dweller = _dwellers; 

    /*Serializing*/ 
    std::stringstream ss; 
    boost::archive::binary_oarchive oa(ss); 
    oa << _house; 
    /*Deserializing*/ 
    boost::archive::binary_iarchive ia(ss); 
    house _house2; 
    ia >> _house2; 
}