问题
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