
In this example, we have a class A that doesn't have a default constructor. An int is needed by the constructor, which is stored in the i member variable. load/save_construct_data are used to un/serialize the i member, while the serialize member is used for all other members.
Looking at this test, I see a couple of behaviors that puzzle me.
-) Saving the 'a' instance in the save() function doesn't end up calling save_construct_data. This leaves a serialized version of A which lacks the 'i' member. This makes it impossible to fully restore the 'a' instance. It seems problematic to me that save_construct_data isn't called in this case.
I just re-ran test_non_default_ctor. I ran under the VC 7.1 debugger and inserted traps inside both save_construct_data and load_construct_data. II verified that save_construct_data and load_construct_data are being invoked exactly as they are intended. I see no problem here.
In my planned usage, I'd like to fully serialize all members, regardless if the objects are serialized directly or through pointers. I cannot see how to do this without serializing the members needed to reconstruct the object in both serialize and save_construct_data, even if some members will end up serialized twice when an object is stored through a pointer.
If the members are not pointers, the constructor is never called. so the parameters set by the constructor are not needed. save_load_contruct_data is only called when its necessary to create a new object.
What's the proper way to serialize all members of a class without a default constructor so that it gets fully serialized regardless how the objects are managed?
Exactly as described in the manual and illustrated by the example test_non_default_ctor.
-) When restoring the 'a' instance within load(), in order to restore it, we first create another 'a' instance (with a different constructor argument), then restore the other members within, leaving untouched the 'i' member. Assuming I'd like to be able to serialize and unserialize completely objects that do not have a default constructor, I'm wondering how I can fully restore an object that doesn't have a default constructor.
All you have to do is define save_construct_data and load_construct_data for all types which don't have a default constructor. The library takes care of everything else.
I've looked for some other mechanism relying on a copy constructor and something along the lines of load_construct_data, but I haven't found any.
Somehow I'm thinking you're looking for the wrong thing. I wonder if you're not underestimating what the library does for you. As I said, you, only need to define save/load_construct_data for those types which don't have a default constructor - there is nothing else required to do.
Robert Ramey
Hello,
As this discussion is going, it seems clear that either I'm missing
something somewhere or that I'm not being clear enough in exposing my
problems... I'll directly include a sample of something similar to what
I'd like to achieve that doesn't work. Please take a look at the
example, and let me know how you would modify this to make both assert
succeed. If you notice that I've missed some concept in the way the
library should be used, please enlighten me!
Thanks,
Martin
------------------------------------------------------------------------
#include <cstdlib>
#include <cassert>
#include <sstream>
#include