C++ Serialization

This is a discussion on C++ Serialization within the C++ Programming forums, part of the General Programming Boards category; For various reasons I would like to be able to serialize some C++ classes that contain various arrays and other ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    129

    C++ Serialization

    For various reasons I would like to be able to serialize some C++ classes that contain various arrays and other objects. In fact what I'm trying to do is to save state, contained in some nested classes and restart my program so that I can do some debugging.

    My question is, is there an automated technique or perhaps some cut and paste boilerplate code or templates for achieving serialization and deserialization in C++? Do I have to write my own for every class?

    Some languages provide some automation for this task.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There's no automatic method for doing this, but generally, you can do this:
    Code:
    class subclass
    {
    private:
        int x;
        int y;
        ...
        friend ostream &operator<<(ostream &os, const subclass &sub);
        friend istream &operator>>(ostream &is, subclass &sub);
    };
    
    ostream &operator<<(ostream &os, const subclass &sub)
    {
        os << sub.x << " " << sub.y << endl;
        return os;
    }
    
    istream &operator>>(istream &is, subclass &sub)
    {
       is >> sub.x >> sub.y;
       return is;
    }
    
    class mainclass
    {
    private:
       subclass arr[10];
       std::string s;
    
       friend ostream& operator<<(ostream &os, const mainclass &m);
       friend istream& operator>>(istream &os, mainclass &m);
    }
    
    friend ostream& operator<<(ostream &os, const mainclass &m)
    {
        for(i = 0; i < 10; i++)
           os << arr[i];
        os << string;
    }
    
    friend istream& operator>>(istream &os, mainclass &m)
    {
        for(i = 0; i < 10; i++)
           os >> arr[i];
        getline(os, string);
    }
    So, you let each subclass output itself, making the overall serialization simpler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    129
    OK I understand and it makes sense. I'm curious though, when you output a numeric array via the stream operators << , are they saved as text or binary by default?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by SevenThunders View Post
    OK I understand and it makes sense. I'm curious though, when you output a numeric array via the stream operators << , are they saved as text or binary by default?
    Text, since << and >> are text extraction/insertion operators.


    You may want to look into Boost's serialization facilities. They use the same operator for serialization and deserialization ('&') which means you don't have to write two functions which look identical to each other except for the '<<' vs '>>'

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Boost.Serialization provides many other advantages over plain iostream overloading, like the ability to resolve circular and multiple references, polymorphic pointers, support for versioning and support for multiple serialization formats.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Boost Serialization unordered_multimap
    By idleman in forum C++ Programming
    Replies: 1
    Last Post: 05-06-2009, 12:14 PM
  2. Serialization problem
    By noobcpp in forum C++ Programming
    Replies: 17
    Last Post: 07-15-2008, 09:01 PM
  3. Boost Serialization: shared_ptr
    By KessiMC in forum C++ Programming
    Replies: 0
    Last Post: 12-26-2007, 08:17 PM
  4. Serialization yay!
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2007, 06:53 PM
  5. serialization and object types
    By Joe Monti in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2003, 11:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21