谁能帮我用 C++ 序列化这个简单的类:
class State {
public:
int count;
Point point;
double angle;
Point* possible;
int possibleSize;
Line line;
list<Point> route;
State() {
}
~State() {
delete[] possible;
}
};
// --- Structures
// Line structure (ax + by + c = 0)
struct Line {
int a;
int b;
int c;
};
// Point structure
struct Point {
int x;
int y;
};
我不能使用任何第 3 方类或库,我需要将其序列化为字节数组(或字符串)。有人可以写吗?我只是不知道如何开始。
非常感谢。
最佳答案
序列化并不神奇。您需要做的就是编写一个函数,以可预测的方式将类的每个变量成员保存到一个数组中,并编写一个匹配函数来读取这样的数组并设置正确的成员。
如果这不是一个类,并且您被允许使用外部库,请考虑查看 Boost Serialization库,尤其是当您需要序列化很多不同的东西时。
关于c++ - 如何序列化这个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4194789/