Saving Classes

Hi,
I'm wondering if there is an automated way to write instances of classes to a disk and read instances of classes from a disk. Does something like sizeof(instanceOfClass) give the actual size of a complete instance of an object?
If so, could fread and fwrite be used directly? i.e. fwrite(&instanceOfClass,1,sizeof(instanceOfClass),outp);
fread(&instanceOfClass,1,sizeof(instanceOfClass),inp);
Sean
That would only work if your class is a POD type.

If you contain anything that has pointers you need to save, then you are to write your own save function.
What is a POD type?
Would standard c++ containers such as map, multimap, vector, set, list, etc... fall into the POD category?
I didn't write the code for those, so I'm not sure if they have pointers or not.
Sean
any complex class with private members is probably not a POD type.

POD types are basic types like int, char, etc... and simple structs consisting of only POD types:

1
2
3
4
5
6
7
8
9
10
11
struct example
{
   int foo;
   int bar;
};  // this struct will be a POD type

struct example2
{
  int foo;
  std::string bar;
};  // this is NOT a POD type because std::string is not a POD 



(note the technical difference between POD and non-POD are slightly different, but the above is a good "rule of thumb" to go by).
http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html

As for the STL containers, no they are not...and unfortunately they don't have any save/load functions so you will have to write your own.
There is no automated way. However, there is a methodology to this. Google around "serialization" for more.
Topic archived. No new replies allowed.