[serialization] Serializing classes with no default constructors
Hi I wonder how can a class that doesn't provide a default constructor can be serialized? As far as I saw, the serialization methods assume that the object is already created. This makes me nervous :), because I wouldn't create an object that is not in a valid state after creation... Am I missing something in this library? Can I do it "the easy way"? Thanks Catalin
At 7:16 PM +0300 5/19/06, Catalin Pitis wrote:
I wonder how can a class that doesn't provide a default constructor can be serialized?
See load_construct_data and save_construct_data, in the documentation section Reference / Serializable Concept / Pointers / Non-default Constructors
This is addressed in the document and there is also an example which demonstrates how this is done. Robert Ramey Catalin Pitis wrote:
Hi
I wonder how can a class that doesn't provide a default constructor can be serialized? As far as I saw, the serialization methods assume that the object is already created. This makes me nervous :), because I wouldn't create an object that is not in a valid state after creation...
Am I missing something in this library? yep Can I do it "the easy way"? yep
Robert Ramey
Thanks Catalin
On May 19, 2006, at 09:16, Catalin Pitis wrote:
I wonder how can a class that doesn't provide a default constructor can be serialized? As far as I saw, the serialization methods assume that the object is already created. This makes me nervous :), because I wouldn't create an object that is not in a valid state after creation...
Am I missing something in this library? Can I do it "the easy way"?
save_construct_data and load_construct data are the correct methods here - although I'm not sure how to use load_construct_data with a class derived from an abstract class. I've gone round on this myself in the last couple of days and decided that unless the object absolutely CANNOT be constructed with a default constructor (e.g., the class holds a reference), it's better to have a private default constructor and a serialize method than to try and use (save|load)_construct_data - mostly for the item cited above about load_construct_data with a derived class. Robert Ramey has mentioned in other messages that default construction with de-serializing is tantamount to "proper" construction anyway, and I agree with that - you need to consider the de-serialization as a form of construction rather than something you do the object after construction.
Hugh Hoover wrote:
On May 19, 2006, at 09:16, Catalin Pitis wrote:
I wonder how can a class that doesn't provide a default constructor can be serialized? As far as I saw, the serialization methods assume that the object is already created. This makes me nervous :), because I wouldn't create an object that is not in a valid state after creation...
Am I missing something in this library? Can I do it "the easy way"?
save_construct_data and load_construct data are the correct methods here - although I'm not sure how to use load_construct_data with a class derived from an abstract class.
I've gone round on this myself in the last couple of days and decided that unless the object absolutely CANNOT be constructed with a default constructor (e.g., the class holds a reference), it's better to have a private default constructor and a serialize method than to try and use (save|load)_construct_data - mostly for the item cited above about load_construct_data with a derived class.
Robert Ramey has mentioned in other messages that default construction with de-serializing is tantamount to "proper" construction anyway, and I agree with that - you need to consider the de-serialization as a form of construction rather than something you do the object after construction.
Hmmm - I don't remember saying that. In any case I feel quite confident that the load_constract_data method is functional, convenient, and even necessary in some cases. I have in reason to believe that it doesn't work with abstract base classes either. The serialization library keeps track of the types of derived pointers. Robert Ramey
On May 20, 2006, at 14:49, Robert Ramey wrote:
Hugh Hoover wrote:
Robert Ramey has mentioned in other messages that default construction with de-serializing is tantamount to "proper" construction anyway, and I agree with that - you need to consider the de-serialization as a form of construction rather than something you do the object after construction.
Hmmm - I don't remember saying that. In any case I feel quite
In the discussion: http://lists.boost.org/boost-users/2005/09/13827.php You don't say exactly that - but that's what I inferred :)
confident that the load_constract_data method is functional, convenient, and even necessary in some cases. I have in reason to believe that it doesn't work with abstract base classes either. The serialization library keeps track of the types of derived pointers.
as near as I can tell, it DOES work for non-abstract derived classes
in my tests (and the examples), but if you try to deserialize like:
Base * pb = NULL;
ar >> pb;
Then you end up in load_construct_data_adl
participants (4)
-
Catalin Pitis
-
Hugh Hoover
-
Kim Barrett
-
Robert Ramey