是否可以在 C++ 中序列化和反序列化一个类? [英] Is it possible to serialize and deserialize a class in C++?

查看:42
本文介绍了是否可以在 C++ 中序列化和反序列化一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 C++ 中对类进行序列化和反序列化?

Is it possible to serialize and deserialize a class in C++?

我已经使用 Java 3 年了,序列化/反序列化在该语言中相当简单.C++ 有类似的功能吗?是否有处理序列化的本机库?

I've been using Java for 3 years now, and serialization / deserialization is fairly trivial in that language. Does C++ have similar features? Are there native libraries that handle serialization?

举个例子会有帮助.

推荐答案

Boost::serialization 库相当优雅地处理了这个问题.我已经在几个项目中使用过它.有一个示例程序,展示了如何使用它,这里.

唯一的原生方式是使用流.这基本上是 Boost::serialization 库所做的一切,它通过设置一个框架来扩展流方法,以将对象写入类似文本的格式并从相同的格式读取它们.

The only native way to do it is to use streams. That's essentially all the Boost::serialization library does, it extends the stream method by setting up a framework to write objects to a text-like format and read them from the same format.

对于内置类型,或者您自己的类型,并正确定义了 operator<<<operator>>,这相当简单;有关详细信息,请参阅C++ 常见问题解答.

For built-in types, or your own types with operator<< and operator>> properly defined, that's fairly simple; see the C++ FAQ for more information.

这篇关于是否可以在 C++ 中序列化和反序列化一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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