Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

can we separate the main serialize method in different class to make it easier and less complex using boost libraries for c++?

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); 
    }  
}; 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...