开发者

How to serialize this class?

开发者 https://www.devze.com 2023-01-25 03:15 出处:网络
can somebody help me with serialization of this simple class in C++: class State { public: int count; Point point;

can somebody help me with serialization of this simple class in 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;
};

I can't use any 3rd party classes or librarie开发者_StackOverflow社区s and I need to serialize this into byte array (or string). Can somebody write how? I just don't know how to start.

Thanks a lot.


Serialization isn't magical. All you need to do is write a function that saves every variable member of the class into an array in a predictable way and a match function to read such an array and set the correct members.

If this isn't for a class and you're allowed to use outside libraries, considering looking into the Boost Serialization library, especially if you'll be needing to serialize lots of different things.


First, figure out how you're going to serialize all the ints and the double. This question has some hints for that. Note that the double is much trickier than the ints. And even though that question and its answers are in C, any C++ serialization would need to use the same central principles.

Two of your members look like variable-length lists. A good way to serialize one of those is to serialize the number of elements (possibleSize or route.size()) followed by each element one at a time.

0

精彩评论

暂无评论...
验证码 换一张
取 消