By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,985 Members | 1,712 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,985 IT Pros & Developers. It's quick & easy.

Saving class members to a binary file

P: n/a
I have some code that works it seems a bit of a hack. I want to know
if there is a more c++y way to do it.

Here's my code:-

class CMyClass{
public:
void Save(CFile fOut);
void Load(CFile fOut);

char *mPointerIDoNotWantTosave;
char *mAnotherPointerIDoNotWantTosave;
....
//start of data i want to save
int mDataStart;
....

int mDataEnd;
} ;
CMyClass::Save(CFile fOut)
{
int dataSize = (char *)mDataEnd - (char *)mDataStart) +
sizeof(mDataEnd);
fOut.Write(&mDataStart, dataSize);
}

I guess I could make my Save and Load functions write each member
individually, but that seems pretty tedious and makes changing the
class a pain. Is there a better way?

Jun 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ml****@gmail.com wrote:
I have some code that works it seems a bit of a hack. I want to know
if there is a more c++y way to do it.

Here's my code:-

class CMyClass{
public:
void Save(CFile fOut);
void Load(CFile fOut);

char *mPointerIDoNotWantTosave;
char *mAnotherPointerIDoNotWantTosave;
...
//start of data i want to save
int mDataStart;
...

int mDataEnd;
} ;
CMyClass::Save(CFile fOut)
{
int dataSize = (char *)mDataEnd - (char *)mDataStart) +
sizeof(mDataEnd);
fOut.Write(&mDataStart, dataSize);
Are you sure that between 'mDataStart' and the end of the chunk you
are saving there are no pointers to dynamic memory? It is possible
that what you're saving does not necessarily allow to restore the
object from file.
}

I guess I could make my Save and Load functions write each member
individually, but that seems pretty tedious and makes changing the
class a pain. Is there a better way?
Not really. "Serialization" is a rather complex issue, but the most
simple part of it is "store only the information that would allow
you to recreate the object when you run your program next time and
read the file; but no less".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 15 '07 #2

P: n/a
Are you sure that between 'mDataStart' and the end of the chunk you
are saving there are no pointers to dynamic memory? It is possible
that what you're saving does not necessarily allow to restore the
object from file.
}
Well I haven't placed any pointers in there - just a few ints, floats
and arrays therof. As I said it works, but I don't know if that is
gauranteed to work with a different compiler...
>
I guess I could make my Save and Load functions write each member
individually, but that seems pretty tedious and makes changing the
class a pain. Is there a better way?

Not really. "Serialization" is a rather complex issue, but the most
simple part of it is "store only the information that would allow
you to recreate the object when you run your program next time and
read the file; but no less".
K Thanks. I had a feeling that might be the answer. I think when I get
the class stable, I'll probably do that.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

Jun 15 '07 #3

P: n/a
On 2007-06-15 19:19, ml****@gmail.com wrote:
I have some code that works it seems a bit of a hack. I want to know
if there is a more c++y way to do it.

Here's my code:-

class CMyClass{
public:
void Save(CFile fOut);
void Load(CFile fOut);

char *mPointerIDoNotWantTosave;
char *mAnotherPointerIDoNotWantTosave;
...
//start of data i want to save
int mDataStart;
...

int mDataEnd;
} ;
CMyClass::Save(CFile fOut)
{
int dataSize = (char *)mDataEnd - (char *)mDataStart) +
sizeof(mDataEnd);
fOut.Write(&mDataStart, dataSize);
}
That seems like a *really* bad idea, the compiler is allowed to put
padding between the members of the class and this padding might be
different on different platforms and even depending on the compiler
options you use.
I guess I could make my Save and Load functions write each member
individually, but that seems pretty tedious and makes changing the
class a pain. Is there a better way?
That depends on your definition of good, but writing each member is
probably the best. There are however other solutions out there, try
searching for serialization..

--
Erik Wikström
Jun 15 '07 #4

P: n/a
Erik Wikström wrote:
That seems like a *really* bad idea, the compiler is allowed to put
padding between the members of the class and this padding might be
different on different platforms and even depending on the compiler
options you use.
My guess is that his intention is to store the contents of the
class only temporarily and the stored data to be read only by the
exact same program in the exact same computer. I think that if that's
the case, it should work ok, although it is indeed a bit of a dirty
hack.
Jun 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.