java - java.io.Serializable 的 C/C++ 等价物是什么?

标签 java c++ c serialization

java.io.Serializable 的 C/C++ 等效项是什么? ?

有对序列化库的引用:

还有:

但这样的等价物是否存在?

因此,如果我在 Java 中有一个如下所示的抽象类,那么 C/C++ 中的可序列化类会是什么样子呢?

import java.io.Serializable;

public interface SuperMan extends Serializable{

    /**
     * Count the number of abilities.
     * @return
     */
    public int countAbility();

    /**
     * Get the ability with index k.
     * @param k
     * @return
     */
    public long getAbility(int k);

    /**
     * Get the array of ability from his hand.
     * @param k
     * @return
     */
    public int[] getAbilityFromHand(int k);

    /**
     * Get the finger of the hand.
     * @param k
     * @return
     */
    public int[][] getAbilityFromFinger(int k);

    //check whether the finger with index k is removed.
    public boolean hasFingerRemoved(int k);

    /**
     * Remove the finger with index k.
     * @param k
     */
    public void removeFinger(int k);

}

是否可以像在 Java 中那样继承任何可序列化的 C/C++ 对象?

最佳答案

没有像Java 那样实现序列化 的标准库类。有一些库可以促进序列化,但对于基本需求,您通常通过重载插入提取 使您的类可序列化> 运营商是这样的:

class MyType
{
    int value;
    double factor;
    std::string type;

public:
    MyType()
    : value(0), factor(0.0), type("none") {}
    MyType(int value, double factor, const std::string& type)
    : value(value), factor(factor), type(type) {}

    // Serialized output
    friend std::ostream& operator<<(std::ostream& os, const MyType& m)
    {
        return os << m.value << ' ' << m.factor << ' ' << m.type;
    }

    // Serialized input
    friend std::istream& operator>>(std::istream& is, MyType& m)
    {
        return is >> m.value >> m.factor >> m.type;
    }
};

int main()
{
    std::vector<MyType> v {{1, 2.7, "one"}, {4, 5.1, "two"}, {3, 0.6, "three"}};

    std::cout << "Serialize to standard output." << '\n';

    for(auto const& m: v)
        std::cout << m << '\n';

    std::cout << "\nSerialize to a string." << '\n';

    std::stringstream ss;
    for(auto const& m: v)
        ss << m << '\n';

    std::cout << ss.str() << '\n';

    std::cout << "Deserialize from a string." << '\n';

    std::vector<MyType> v2;

    MyType m;
    while(ss >> m)
        v2.push_back(m);

    for(auto const& m: v2)
        std::cout << m << '\n';

}

输出:

Serialize to standard output.
1 2.7 one
4 5.1 two
3 0.6 three

Serialize to a string.
1 2.7 one
4 5.1 two
3 0.6 three

Deserialize from a string.
1 2.7 one
4 5.1 two
3 0.6 three

序列化格式完全取决于程序员,您有责任确保要序列化的类的每个成员本身可序列化(定义了一个插入/提取运算符)。您还必须处理字段的分隔方式(空格或换行符或零终止?)。

所有基本类型都有预定义的序列化(插入/提取)运算符,但您仍然需要注意诸如 std::string 可以包含(例如)空格或换行符(如果您使用空格或换行符作为字段分隔符)。

关于java - java.io.Serializable 的 C/C++ 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37720431/

相关文章:

java - 向 Java BooleanProperty 类添加自定义方法

java - 未找到 HanderMapping - Spring 3.x - Controller

java - 通知 android 显示日期

c++ - Codeblocks 编译,GCC 不编译

java - 使用 LWJGL 和 PNGDecoder 加载纹理时天空盒呈现黑色

c++ - 在指针声明中放置星号

c++ - Makefile 不读取其余部分

c - 如何将整数指针传递给函数?

c++ - 如何通过 USB 访问 iPhone 文件系统(从 Windows)

c - C 中的二进制文件读写