Saving class members to a binary file

M

mlamb1

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?
 
V

Victor Bazarov

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
 
M

mlamb1

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...
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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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..
 
J

Juha Nieminen

Erik said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top