我们可以区分不同类的主要序列化方法,使之更容易,更复杂的使用boost库C ++? [英] can we separate the main serialize method in different class to make it easier and less complex using boost libraries for c++?

查看:132
本文介绍了我们可以区分不同类的主要序列化方法,使之更容易,更复杂的使用boost库C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何序列化方法从此code分离,并将其封装到另一个类,这样我们就不必把它写在每一个我们创建的类。

 类测试
{
私人的:
    友元类的boost ::系列化::访问;
   模板<类归档和GT;无效连载(归档和放大器; AR,
            const的无符号整型版)
    {
        AR&安培; BOOST_SERIALIZATION_NVP(一);
        AR&安培; BOOST_SERIALIZATION_NVP(二);
        AR&安培; BOOST_SERIALIZATION_NVP(EMP);
    }    int类型的;
    INT B:
    员工* EMP;
上市:
    测试(INT A,INT B,INT C,INT D)
    {
         这 - >一种=一个;
         这 - > B =;
        EMP =新员工(C,D);
    }
};


解决方案

由于写的的文档您可以使用免费的功能序列化类。当然,这要求数据成员是公开的。

有关真正的类,隐藏自己的数据,你可以定义获取和出真正的类通过了序列化目的的序列结构。

how to separate the serialize method from this code and encapsulate it to a another class so that we don't have to write it in every class we create.

class Test  
{  
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);  
        ar & BOOST_SERIALIZATION_NVP(emp); 
    }  

    int a; 
    int b; 
    Employee *emp; 
public:  
    Test(int a, int b,int c, int d)  
    { 
         this->a = a; 
         this->b = b; 
        emp = new Employee(c,d); 
    }  
}; 

解决方案

As written in the docs you can use a free function to serialize a class. Of course this requires the data members to be public.

For "real" classes that hide their data you could define a "serialization struct" that gets passed in and out of the real class for serialization purposes.

这篇关于我们可以区分不同类的主要序列化方法,使之更容易,更复杂的使用boost库C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆