gpt4 book ai didi

c++ - 使用多态存档 boost 序列化

转载 作者:可可西里 更新时间:2023-11-01 16:35:21 26 4
gpt4 key购买 nike

我正在开发一个使用 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, int _d) : base(_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;
}

关于c++ - 使用多态存档 boost 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/478668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com