Re: Difference between struct and class and pointer to member data
On Sep 9, 4:03 pm, Michael Tsang <mikl...@gmail.com> wrote:
[...]
Difference between class and struct:
class has private access level, struct has public access
level; class can be used to declare template parameter, struct
can't.
The word class has two meanings: it is a keyword (which can be
used to declare a template type parameter, in which case it is
synonymous with typename), but the standard also talks about
class types. I suspect that this is often a source of
confusion---a class type can be declared with any of the
keywords class, struct or union (but there are a number of
constraints regarding the use of union).
When you serialize (dump) a class into a file, the content
will be the internal representation of a class (including any
v-table, padding bytes, etc.).
Serializing is *not* dumping. Dumping is only valid for a very
small set of types, at least if you want to reread the data
later. (Dumping is usually only done in hex format, for
information purposes.)
Therefore, dumping any class with pointers (including all
classes with virtual members and inheritance, as they have a
hidden pointer at the front) is useless. Deserializing a class
(providing it does not have any pointers) is similar to
serializing a class, you only need to pass the pointer and the
size. Therefore, to deserialize a class, you just need to call
fread(3).
That's only true for a very limited number of cases: character
types, and arrays of character types.`
Here is the criteria for classes which are safe to serialize:
You can serialize any type you want, including all class types,
but it's up to you to write the code to do it.
--
James Kanze