2014-03-24 84 views
0

這段代碼必須在我們每次創建類時寫入,即從template<class archive>ar & BOOST_SERIALIZATION_NVP(b)。我們怎樣才能縮短它? 以及我們如何序列化stl容器?使用boost序列化來序列化類而不更改類

class Employee 
{ 
private:  
    friend class boost::serialization::access; 
    template<class Archive> void serialize(Archive & ar, 
      const unsigned int version) 
    { 
     ar & BOOST_SERIALIZATION_NVP(a); 
     ar & BOOST_SERIALIZATION_NVP(b); 
    } 

    int a; 
    int b; 

public: 
    Employee(int a, int b) 
    { 
     this->a = a; 
     this->b = b; 
    } 

}; 
+0

的可能重複[我們可以在不同的分離主序列化方法類型的通用包裝類使更容易和更簡單的使用boost庫C++?](http://stackoverflow.com/questions/22603039/can-we-separate-the-main-serialize-method-in-different-class-to-讓它更容易a) – jfly

回答

0

我建議你先從文檔:)

STL容器是序列化的,當你有相關的頭(下序列化的概念開始):

#include <boost/serialization/map.hpp> 
#include <boost/serialization/string.hpp> 

開箱即用的支持其他東西。

可以讓那些已經取得了「reflectible」通過其他方式(如融合序列,Qt的QObject對象等)

+0

這我知道我們有頭文件包含在這個,但他們如何實際上他們之間的鏈接,即他們顯然不會改變標題中的代碼,假設std :: vector。 所以它們如何實際上創建了boost序列化和vector之間的鏈接,例如boost/serialization/vector.hpp ??????? – shivi

+0

@shivi我不知道你想知道什麼,這不在樣本中。你可以 - 也許 - 找到一個更直接的例子[這裏](http://stackoverflow.com/questions/22533863/boost-serialization-of-native-type-defined-with-typedef/22533995#22533995)。只包括標題使OP的代碼編譯 – sehe

+0

毫米。也許你想知道'serialize()'如何知道它正在處理一個向量?那麼,這是簡單的重載分辨率:'foo(int); template foo(std :: vector const&);'會自動爲'foo(42)'調用正確的重載''foo(my_vector)'' – sehe