0

Im trying to text serialize and deserialize an object containing a container of abstract objects in c++,does somebody know of a code example of the above?

  • What are abstract objects? Since abstract classes are classes from which objects cannot be instantiated, the term "abstract objects" sounds surprising to me. – Daniel Daranas Jul 13 '09 at 20:07
  • I believe that's dependent on the container & objects themselves. From my understanding you must be able to serialize each of them so that you can serialize your object. IE: You serialize your object, which would serialize the container, which would serialize each of the objects it contains. – dborba Jul 13 '09 at 20:07
  • @Daniel - I think what he means is that the container is unware of the objects' types that it contains. I might be wrong however. – dborba Jul 13 '09 at 20:08
  • @DB - On a second reading, I think you're right. – Daniel Daranas Jul 13 '09 at 20:10

4 Answers4

3

Take a look at boost::serialize.

It contains methods to assist in the serialization of containers (link loses frame on left).

Of course, don't just skip to that page, you'll want to read the whole thing. :)

GManNickG
  • 494,350
  • 52
  • 494
  • 543
2

Unlike other languages, C++ doesn't come with this kind of serialization "baked in." You're going to want to use a library. Such as Boost.Serialization, Google Protocol Buffers (can be a file format) or Apache Thrift.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
1

You could create a method for your abstract class called:

virtual void serialize(char *out, int outLen) = 0;

.. and in turn a static deserializer:

AbstractClass deserialize(char *serializedString, int strLen);

In your deserializer, you could have different strategies to deserialize the right subclass of the abstract class.

cwap
  • 11,087
  • 8
  • 47
  • 61
1

Hey I asked a similar question a little while back. Have a look at dribeas's answer it was particularly good. This method allows the addition of new objects of the abstract type will little manipulation of existing code (ie. we can serialize them without adding additional switch/else if options to our deserializer).

Best Practice For List of Polymorphic Objects in C++

Community
  • 1
  • 1
DeusAduro
  • 5,971
  • 5
  • 29
  • 36