Writing Classes to File

Jeremy Jeremy_member at pathlink.com
Mon May 8 20:32:11 PDT 2006


In article <e3o138$1ujn$1 at digitaldaemon.com>, Jeremy says...
>
>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 )
>
>

Just for everyone who reads this later -- the above works. I can write the
entire class to a file for later reading/reconstruction with only that
writeExact function call. Ahhh, my saving/loading troubles just got much easier
:)





More information about the Digitalmars-d mailing list