Re: [Dev-C++] Copy Constructors and Overloaded Assignment Operators
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Abhijit S. <mu...@gm...> - 2002-07-21 09:11:51
|
> Now, speaking of class members, how would you persist state?
>
> In VB (the realm I come from... well, there was Pascal those many
> years ago 8-), I would use a Type (read struct) to hold my data.
> This allows for quick memory copies to restore defaults/back out
> changes... In C++ I can also see it making serialization a little
> easier (convert to a char array/string and dump to disk).
This isn't always portable, since many compilers use memory alignment to
optimize access... Consider this:
struct foo
{
char bar; // sizeof(char) is 1
int tender; // sizeof(int) is 4
};
sizeof(foo) may not be 5. By default, in most 32-bit optimizing compilers
(including MingW and BC), it'll be 8, since all members are aligned on
dword-boundaries. So if you try to read in the struct using a version of the
program which has been compiled without this optimization, you won't get
what you expected. Neither will your son, when he tries reading it in from a
64-bit compiled program. A more portable way to persist is methods such as
saveToDisk() and loadFromDisk(), which read and write each member. This way
you can also change the read/write method to suit the platform... on
Windows, it can use the registry, on Linux the home dir, on DOS a config
file, etc. You could also interface to classes like FooWriter and FooReader,
to which you can pass the struct/class as an argument.
Hope this helps.
|