简体   繁体   中英

Is it possible to Serialize and Deserialize objects in C++?

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?

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

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.

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.

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.

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.

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM