提升:包含指针类实例的序列化

编程入门 行业动态 更新时间:2024-10-24 09:25:12
本文介绍了提升:包含指针类实例的序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我序列化类的一个实例的集装箱,使用Boost库:

If I serialize an instance of the class Container, using Boost library:

class Container { public: Block** blocks; int blocksNumber; } class Block { public: int blockType; unsigned char* data; }

然后填写所有必要的数据,以得到一个完整的容器:

And then filling all the necessary data to get a complete Container:

Container container = new Container(); container.blocksNumber=5; Block** blocks = (Block**)malloc(sizeof(Block*)*container.blocksNumber); blocks[0] = (Block*)malloc(sizeof(Block)); //..... Filling all the necessary data... and constructing the container

所以我的问题是:实例的​​序列化格式的容器包含所有分配的数据?换句话说,如果我反序列化容器的序列化格式,将我能够阅读的数据内容?谢谢了很多!

So my question is: Would the serialized format of the instance container contain all the allocated data? In other words, if I deserialize the serialized format of container, would I be able to read the content of data? Thank you a lot!

推荐答案

我不打算为什么你这里带来不安全Ç大炮浪费任何时间考虑。你打算使用升压序列化,现代化通用的C ++库。

I'm not going to waste any time contemplating why you bring unsafe C cannons in here. You're looking to use Boost Serialization, a modern generic C++ library.

下面是它应该是什么样子:

Here's what it should look like:

struct Block { int blockType; std::vector<uint8_t> data; }; struct Container { std::vector<std::vector<Block>> block_lists; };

看到一个完整的工作示例:

See a complete working sample:

住在Coliru

#include <boost/serialization/vector.hpp> class Block { public: int blockType; std::vector<uint8_t> data; private: friend class boost::serialization::access; template <typename Ar> void serialize(Ar& ar, unsigned) { ar & blockType; ar & data; } }; class Container { public: std::vector<std::vector<Block>> block_lists; private: friend class boost::serialization::access; template <typename Ar> void serialize(Ar& ar, unsigned) { ar & block_lists; } }; #include <boost/archive/text_oarchive.hpp> int main() { Container const letshavesome = { { // block_lists { Block { 1, { 0x11, 0x12, 0x13, 0x14 } }, Block { 2, { 0x21, 0x22, 0x23, 0x24 } }, Block { 3, { 0x31, 0x32, 0x33, 0x34 } }, }, { Block { 4, { 0x41, 0x42, 0x43, 0x44 } }, Block { 5, { 0x51, 0x52, 0x53, 0x54 } }, Block { 6, { 0x61, 0x62, 0x63, 0x64 } }, }, } }; boost::archive::text_oarchive oa(std::cout); oa << letshavesome; }

输出

22 ::系列化存档11 0 0 0 0 2 0 0 0 3 0 0 0 1 0 4 17 18 19 20 2 4 0 33 34 35 36 3 4 0 49 50 51 52 3 0 4 4 0 65 66 67 68 5 4 0 81 82 83 84 6 4 0 97 98 99 100

更多推荐

提升:包含指针类实例的序列化

本文发布于:2023-08-04 05:58:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   实例   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!