Reading and writing a vector of classes

安稳与你 提交于 2020-01-05 08:43:50

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/22569522/reading-and-writing-a-vector-of-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!