读和写类的向量 [英] Reading and writing a vector of classes

查看:266
本文介绍了读和写类的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个三级/四级的继承层次结构。并且在每个级别内将至少保存一个或多个具有不同属性的类,使类的对象唯一(当然,从上面的级继承属性)。

一个类的每个对象可能有不同的属性,因此我的问题是,如何读取和写入每个对象到一个文件,并区分属性?



解决方案

这个问题可怕地低估了,我只是建议使用图书馆。



我个人非常喜欢Boost序列化




  • 功能集(对象跟踪,智能指针,别名检测,循环图,多态类型和集合)

  • 非侵入性(您可以向3rdparty类型)

  • 可移植性(需要注意的是,您不应期望标准二进制文件可以跨机器/版本移植:use EOS便携式存档



查看示例(其具有多态集合):http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc /tutorial.html#examples



海中有更多的鱼:protobuf据说是非常棒的。



您/可以/使用Spirit Qi / Karma制作您自己的曲目,虽然我不建议这样做,除非您需要人工可读的格式


I have an inheritance hierarchy with three/four levels. And within each level will hold at least one or more classes with different attributes that make that object of a class unique (and of course, inheriting attributes from the level above).

Each object of a class may have different attributes to another therefore my question is, how can I read and write each object to a file and differentiate the attributes?

I do apologise if I have not worded this very well but will be very appreciative if you can help.

解决方案

The problem being horribly underspecified, I'm just going to suggest using a library.

I'm personally quite fond of Boost Serialization for

  • feature set (object tracking, smart pointers, alias detection, cyclic graphs, polymorphic types and collections)
  • non-intrusiveness (you can add serializability to 3rdparty types)
  • portability (with the caveat that you should not expect the standard binary archive to be portable across machines/version: use EOS Portable Archives for that

See the sample (which has polymorphic collections): http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/tutorial.html#examples

There are more fish in the sea: protobuf is supposedly quite awesome.

You /can/ roll your own using Spirit Qi/Karma, though I'd not suggest this unless you want human readable formats

这篇关于读和写类的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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