c – 使用多态存档的Boost序列化

前端之家收集整理的这篇文章主要介绍了c – 使用多态存档的Boost序列化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一个客户端 – 服务器应用程序,它使用boost :: serialization库来满足它的序列化需求.

我需要对似乎不起作用的多态对象进行序列化和反序列化.文档确实表示它是受支持的,但没有相关示例显示我在这里做的工作.所以,我不太确定.我的问题是可以使用boost来序列化/反序列化多态对象吗?如果是,我在这里做错什么?

谢谢!

码:

using namespace std;  

class base {  
  public:
    int data1;  

    friend class boost::serialization::access;  

    void serialize(boost::archive::polymorphic_iarchive & ar,const unsigned int file_version) {  
        ar & data1;  
    }  

    void serialize(boost::archive::polymorphic_oarchive & ar,const unsigned int file_version){  
        ar & data1;  
    }  

  public:  
    base() {};  
    base(int _d) : data1(_d) {}  
    virtual void foo() const {std::cout << "base" << std::endl;}  
};  

class derived : public base {  
  public:  
    int data2;  

    friend class boost::serialization::access;  

    void serialize(boost::archive::polymorphic_iarchive & ar,const unsigned int file_version) {  
        ar & boost::serialization::base_object<base>(*this) & data2;  
    }  

    void serialize(boost::archive::polymorphic_oarchive & ar,const unsigned int file_version){  
        ar & boost::serialization::base_object<base>(*this) & data2;  
    }  

  public:  
    derived() {};  
    derived(int _b,int _d) : base(_b),data2(_d) {}  
    virtual void foo() const {std::cout << "derived" << std::endl;}  
};  

int main(int argc,char *argv[]) {  
    // client  
    const base *b1 = new derived(1,2);  

    std::ostringstream oss;  
    boost::archive::polymorphic_text_oarchive oa(oss);  
    oa << *b1;  

    // server  
    base *b2 = new derived(3,4);  

    std::istringstream iss(oss.str());  
    boost::archive::polymorphic_text_iarchive ia(iss);  
    ia >> *b2;  

    // prints 1,ok  
    cout << b2->data1 << endl;  

    // prints 4,why wasn't the derived class data written?
    cout << (dynamic_cast<derived*>(b2))->data2 << endl;  

    return 0;  
}

解决方法

找到一个决议.我不得不使用语句导出派生类:
BOOST_CLASS_EXPORT(derived);

发布一些适用于某些更正的内容.

using namespace std;  

class base {  
  public:  
    int data1;  

    friend class boost::serialization::access;  

    template<typename Archive>  
    void serialize(Archive & ar,const unsigned int file_version) {  
        ar & data1;  
    }  

  public:  
    base() {};  
    base(int _d) : data1(_d) {}  
    virtual void foo() const {std::cout << "base" << std::endl;}  
};  

class derived : public base {  
  public:  
    int data2;  

    friend class boost::serialization::access;  

    template<typename Archive>  
    void serialize(Archive & ar,const unsigned int file_version) {  
        ar & boost::serialization::base_object<base>(*this);  
        ar & data2;  
    }  

  public:  
    derived() {};  
    derived(int _b,data2(_d) {}  
    virtual void foo() const {std::cout << "derived" << std::endl;}  
};  

BOOST_CLASS_EXPORT(derived);  

int main(int argc,char *argv[]) {  
    // client  
    // Assign to base type  
    std::unique_ptr<const base> b1(new derived(1,2));  

    std::ostringstream oss;  
    boost::archive::text_oarchive oa(oss);  
    oa & b1.get();   

    // server  
    // Retrieve derived type from base  
    std::unique_ptr<base> b2;

    std::istringstream iss(oss.str());
    boost::archive::text_iarchive ia(iss);
    {
        base *temp; 
        ia & temp;
        b2.reset(temp);
    }
    cout << b2->data1 << endl;  
    cout << (dynamic_cast<derived*>(b2.get()))->data2 << endl;  

    return 0;  
}
原文链接:https://www.f2er.com/c/113202.html

猜你在找的C&C++相关文章