Writing Classes to File

Jeremy Jeremy_member at pathlink.com
Mon May 8 20:35:42 PDT 2006


In article <Pine.LNX.4.64.0605082013370.2422 at bellevue.puremagic.com>, Brad
Roberts says...
>
>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

Hrm... So after I write the class, I should also write the char[]'s seperately
with another write function. This shouldn't be too much of a problem... my class
consists mostly of floats/ints/doubles





More information about the Digitalmars-d mailing list