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

查看:273
本文介绍了是否可以序列化和反序列化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 库处理这个相当优雅。我已经在几个项目中使用它了。

The Boost::serialization library handles this rather elegantly. I've used it in several projects.

编辑1:有一个示例程序,显示如何使用它,这里

EDIT 1: There's an example program, showing how to use it, here.

编辑2:唯一的本地方式它是使用流。这本质上是所有的Boost :: serialization库,它通过设置一个框架来将对象写成类似于文本的格式并从同一格式读取它来扩展流方法。对于内置类型,或者对于具有操作符<<<和运算符>>正确定义,这是相当简单;有关详细信息,请参见 C ++常见问题解答Lite

EDIT 2: 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. For built-in types, or your own types with operator<< and operator>> properly defined, that's fairly simple; see the C++ FAQ Lite for more information.

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

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