Writing Classes to File

Brad Roberts braddr at puremagic.com
Mon May 8 20:15:22 PDT 2006


On Mon, 8 May 2006, Jeremy wrote:

> In article <e3o0d4$1tdc$1 at digitaldaemon.com>, Sean Kelly says...
> >
> >Jeremy wrote:
> >> Is there a way I can just write a whole class to a file?
> >> 
> >> For example, why can't I do:
> >> 
> >> file.writeExact(someClass, someClass.sizeof);
> >> 
> >> This seems to just write 4 bytes, which must be the pointer.
> >> 
> >> The compiler complains when I do this:
> >> 
> >> file.writeExact(*someClass, (*someClass).sizeof);
> >> 
> >> Saying that '*' is only used infront of pointers -- but aren't class always
> >> represented with pointers?
> >
> >They are, but I think they don't have the same semantics as pointers 
> >insofar as the type is concerned.  This is what inspired me to make the 
> >"isizeof" proposal a few weeks ago, as there's currently no way to get a 
> >class' size at compile-time.  However, you can get it a run-time via:
> >
> >someClass.classinfo.init.length
> >
> >
> >Sean
> 
> Interesting... so could this be done:
> 
> file.writeExact(someClass, someClass.classinfo.init.length);
> 
> to write the entire class to a file? (I'd like to do some zlib compression on it
> too :-D )

Even with this, direct writing of a class like this will only work in the 
simplest of cases.  It won't dive into sub structures, for example, 
char[]'s.  Object serialization for the general case is a lot more 
complicated.

Later,
Brad



More information about the Digitalmars-d mailing list