Reputation: 2665
Is it possible in boost::serialization library to deserialize (polymorphic) objects with references and no default constructor?
class Example
{
int& value;
public:
Example(int _value): value(_value) {}
virtual ~Example() {}
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & value;
}
};
class Usage
{
Example* example;
public:
Usage(): example(new Example(123)) {}
~Usage() { delete example; }
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive & ar, const unsigned int file_version)
{
ar & example;
}
};
...
// serialize and deserialize object with reference and no default constructor
{
Usage source;
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa & source;
Usage target;
std::istringstream iss(oss.str());
boost::archive::text_iarchive ia(iss);
ia & target; // does not compile
}
Upvotes: 2
Views: 2481
Reputation: 11659
As for non-default constructible object, I'd recommend to look the item
Non-Default Constructors
here.
Your class can be serialized by
writing your own function template load_construct_data
and save_construct_data
.
Upvotes: 5