Reputation: 604
I used this method to serialize my object :
void save(Obj& obj) {
ofstream os("obj.dat", ofstream::binary);
boost::archive::binary_oarchive ar(os, boost::archive::no_header);
ar << boost::serialization::make_binary_object(&obj, sizeof(obj));
}
What would be my the code for my Obj load(string fileName)
?
Upvotes: 1
Views: 118
Reputation: 393114
It's basically the same as what you had:
Obj load(std::string const& filename) {
std::ifstream is(filename, std::ios::binary);
boost::archive::binary_iarchive ar(is, boost::archive::no_header);
Obj obj;
ar >> boost::serialization::make_binary_object(&obj, sizeof(obj));
return obj;
}
Of course, this is assuming that your type is valid for use with make_binary_object
: make sure that Obj
is bitwise serializable (POD):
static_assert(std::is_pod<Obj>::value, "Obj is not POD");
Also, reconsider using namespace
: Why is "using namespace std;" considered bad practice?
Upvotes: 1