Deserializing const fields

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Sep 5 00:18:57 PDT 2012


On 9/5/12 9:08 AM, Jacob Carlborg wrote:
> I hit an interesting problem in my serialization library: deserializing
> const (and immutable) fields. The problem is that const fields need to
> be set in a constructor.
>
> In my current implementation I don't call the constructor at all. The
> reason for this is that I want to be able to (de)serialize classes
> without a default constructor. Instead it's possible to register event
> handlers for when a class is deserialized.
>
> So I think that I either need to require that a class with const fields
> need to have a default constructor or a constructor that takes the
> serializer as an argument and leave the class to manually deserialize
> the const fields in the constructor.
>
> Anyone got any other ideas?

Deserialization is a low-level operation, and it shouldn't invoke a 
constructor because the object has been constructed at serialization time.

I suggest you allocate a chunk of raw memory, deserialize into it, then 
cast it to the object type.


Andrei


More information about the Digitalmars-d mailing list