4

As we know c++ is also an Object Oriented Programming language where most the things are objects like java. So wanted to know is the Serialize and deserializ features are available in c++ as well as we do it in java?

If yes how it can be achieved?

In java We use Serializable Interface to say that this type of object can be serialized and deserialized.

So in c++ how?

And out of curiosity is it same in c# as in java?

GuruKulki
  • 25,776
  • 50
  • 140
  • 201

8 Answers8

2

There is no feature built-in for doing this in C++. You will have to use external libraries, like Boost.

Sameer
  • 725
  • 5
  • 8
1

You could use stack storage without using the 'new' operator so the class is essentially packed like a struct, and then export the entire memory region out to file. When you want it again, allocate the memory region and read the class data back into it from file, and then access that as you would normally. It won't be portable, but it will work. It's quite nice on machines like the Nintendo DS to store level data when you save something using an editor, although certainly not pretty (and dangerous on complex systems, furthermore!)

Note: I'm not recommending that you do this, but it's valid on some embedded platforms, as mentioned. I just wanted to post something interesting, which homebrew devs actually do in C++ on the Nintendo DS when developing using palib.

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
  • I'm not going to downvote, but handheld Nintendo programming practices don't necessarily carry over. Are you saying to pull several different objects out of the stack at once as one glob of memory? – Potatoswatter Mar 21 '10 at 19:15
  • It's an extremely good system for games - e.g. it gives very good level-loading times - but it doesn't really work for a lot of applications. –  Mar 21 '10 at 19:16
  • 1
    This doesn't work if any objects inside the class have stack storage. Meaning now you can't use any STL containers, no PIMPL, no pointers or references at all. – rlbond Mar 21 '10 at 19:19
  • Indeed :) I thought I'd throw this up anyway. By the way, it's possible to have memory allocation using memory on the stack as a heap by overriding the new operator for that class. That way, it may still be possible to use all that stuff (please confirm :)). – Chris Dennett Mar 21 '10 at 19:26
  • 1
    This is an almost dangerous answer... I'm not downvoting it because I think it's pretty cool, but if somebody asks basic questions, perhaps we should give basic answers? – mnemosyn Mar 21 '10 at 19:40
  • That might work for arrays *if* C++ supports a variable-length sizeof (I don't know; I've never tried it) and *if* you can allocate a huge amount of memory on the stack. It would be damned near impossible to get it to work for std::list, std::map, and std::set. And thanks, Chris, for screwing up the Win64 type system. fread/fwrite-style serialization is why Microsoft couldn't make "long" 64 bits like on other OSes. – dan04 Mar 21 '10 at 19:48
1

Java and C# supports reflection. Basically they can find out enough information from the object/class for meaningful automatic serialization/deserialization.

The C++ language has no reflection, for example you cannot iterate thru the fields of a class. So you have to use a more or less manual method. Of course there are libraries like Boost::serialization to help.

mfazekas
  • 5,589
  • 1
  • 34
  • 25
1

Even in java, the Serializable interface is just one approach. I would give good consideration here to protocol buffers; there are java, C++ and C# (/.NET) implementations, and many others; giving you interop / portability in addition to fast, efficient binary serialization.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

generally, you can write a function called serialize(), and then have something as follows:

ofstream outFile;
outFile.open (dirFileString.c_str(), ios::binary);
outFile.write (reinterpret_cast < char *>(&x),
                   sizeof (x));
outFile.write (reinterpret_cast < char *>(&y),
                   sizeof (y));

and then have a similar function to read in:

inFile.read (reinterpret_cast < char *>(&x),
                   sizeof (x));
inFile.read (reinterpret_cast < char *>(&y),
                   sizeof (y));

You can do this sort of thing for as many variables within the class object as you need. Cheers.

Ben J
  • 1,367
  • 2
  • 15
  • 33
0

There are various libraries to support serialization for C++, but it is a somewhat more complex job. Serialization in Java depends on the fact that objects form a single, monolithic tree, which is not the case in C++ at all.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Use Qt4 http://qt.nokia.com/doc/qdatastream.html provide your overrides for operator<< and operator>>

template <class KT, class VT>
inline QDataStream &operator>>(QDataStream &in, SOMap<KT, VT> &map) {
    QDataStream::Status oldStatus = in.status();
    in.resetStatus();
    map.clear();
    quint32 n;
    in >> n;
    for (quint32 i = 0; i < n; ++i) {
        if (in.status() != QDataStream::Ok)
            break;
        KT key;
        VT value;
        in >> key >> value;
        map.append(key, value);
    }
    if (in.status() != QDataStream::Ok)
        map.clear();
    if (oldStatus != QDataStream::Ok)
        in.setStatus(oldStatus);
    return in;
}

template <class KT, class VT>
inline QDataStream &operator<<(QDataStream &out, const SOMap<KT, VT> &map) {
    out << quint32(map.size());
    for(int i = 0, c = map.size(); i < c; ++i) {
        typename SOMap<KT, VT>::pair_type n = map.at(i);
        out << n.first << n.second;
    }
    return out;
}
OneOfOne
  • 95,033
  • 20
  • 184
  • 185