Reputation: 645
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 libraries 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.
Upvotes: 0
Views: 540
Reputation: 72311
First, figure out how you're going to serialize all the int
s and the double
. This question has some hints for that. Note that the double
is much trickier than the int
s. 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.
Upvotes: 0
Reputation: 46913
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.
Upvotes: 6