• Advertisement
Sign in to follow this  

How to serialize

This topic is 4174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a class which includes one or two pointers to arrays - this is so the array size can be declared at runtime. I'm fairly certain this would need to have a special function to serialize it. Can anyone point me to any tutorials about serialization?

Share this post


Link to post
Share on other sites
Advertisement
If you "must" do it yourself, this is the best I can offer you.

However, boost::serialization is highly recommended.

Also, why not just use std::vectors? (You'll still need to make special provisions to serialize them, but you will save yourself huge amounts of memory management work).

Share this post


Link to post
Share on other sites
Well I'm actually making a map class - using multidimensional arrays for tiles. I'm not too sure if you can have multidimensional vectors but if you did I'd think that they'd be pretty complex.

Share this post


Link to post
Share on other sites
Quote:
Original post by Peter Conn
I'm not too sure if you can have multidimensional vectors but if you did I'd think that they'd be pretty complex.


Just as complex as dynamic multidimensional arrays. To make things easier you can use a one-dimensional vector to emulate however many dimensions you want and use an indexing trick:

int **array;
vector<int> vec;
array[j] becomes vec[i * width + j]

int ***array;
vector<int> vec;
array[j][k] becomes vec[i * width * height + j * width + k]

If that looks too ugly, you can use use a "multidimensional" vector:
vector< vector<int> >; //2D
vector< vector< vector<int> > >; //3D

By the way, if you will be using boost::serialization, vectors are preferred over pointers because boost already knows internally how to serialize vectors.

Share this post


Link to post
Share on other sites
Thanks, I'll do that.

For furture reference how would you serialize a pointer, get the value it points to, save that, load it and then use "new" on the pointer?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement