Suppose I have the following:
class dataClass {
public:
int someData;
float moreData;
void setData();
};
dataClass data;
What would happen if I called fwrite(&data, sizeof(data), 1, outputFilePointer);? Would the code act as if dataClass had no functions, or would I have to call fwrite() with each member?
In this very case, your class is POD, so it would work exactly as it would if it was a Plain Old C struct, i.e. it will dump the memory representation of
dataon disk (including the padding bytes that the compiler may insert).However, if
virtualmethods,virtualinheritance and other C++ stuff kicks in, you may start to see strange stuff (you will see not only your normal data fields, but also the pointer to the vtable and maybe other stuff put in automatically by the compiler); I think that also multiple inheritance may add confusion.Notice however that just calling
fwriteon an object should be harmless in every case (although it may be unspecified behavior, but I didn’t check); problems instead may arise if you instead try to deserialize a non-POD object from file with just anfread(for example, the correct vtable pointer may be overwritten by the one stored in the file, that can be no longer valid, and this will make everything blow up at the next virtual call).