简体   繁体   English

是否可以在C ++中序列化和反序列化对象?

[英]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. 我们知道c ++也是一种面向对象的编程语言,其中大多数东西都是像java这样的对象。 So wanted to know is the Serialize and deserializ features are available in c++ as well as we do it in java? 所以想知道Serialize和deserializ功能是否可以在c ++中使用,我们在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. 在java中我们使用Serializable接口来说明这种类型的对象可以被序列化和反序列化。

So in c++ how? 那么在c ++中如何?

And out of curiosity is it same in c# as in java? 出于好奇,它在c#和java中一样吗?

There is no feature built-in for doing this in C++. 在C ++中没有内置的功能。 You will have to use external libraries, like Boost . 您将不得不使用外部库,如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. 您可以在不使用'new'运算符的情况下使用堆栈存储,因此该类基本上像结构一样打包,然后将整个内存区域导出到文件。 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!) 当你使用编辑器保存某些东西时,在Nintendo DS这样的机器上存储水平数据是相当不错的,虽然肯定不漂亮(而且在复杂的系统上也很危险!)

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. 我只是想发布一些有趣的东西,当使用palib进行开发时,自制开发人员在Nintendo DS上用C ++实际做了。

Java and C# supports reflection . Java和C#支持反射 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. C ++语言没有反射 ,例如,您无法通过类的字段进行迭代。 So you have to use a more or less manual method. 所以你必须使用或多或少的手动方法。 Of course there are libraries like Boost::serialization to help. 当然有像Boost :: serialization这样的库可以提供帮助。

Even in java, the Serializable interface is just one approach. 即使在java中, Serializable接口也只是一种方法。 I would give good consideration here to protocol buffers ; 我会在这里考虑协议缓冲区 ; there are java, C++ and C# (/.NET) implementations, and many others ; 有java,C ++和C#(/ .NET)实现, 以及许多其他实现 ; 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: 通常,您可以编写一个名为serialize()的函数,然后执行如下操作:

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. 有各种库来支持C ++的序列化,但它是一个更复杂的工作。 Serialization in Java depends on the fact that objects form a single, monolithic tree, which is not the case in C++ at all. Java中的序列化取决于对象形成单个整体树的事实,而C ++中根本不是这种情况。

Use Qt4 http://qt.nokia.com/doc/qdatastream.html provide your overrides for operator<< and operator>> 使用Qt4 http://qt.nokia.com/doc/qdatastream.html为操作符<<和操作符>>提供覆盖

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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