1

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) ?

PepperTiger
  • 564
  • 3
  • 8
  • 21
  • 1
    What does [the documentation](https://www.boost.org/doc/libs/1_72_0/libs/serialization/doc/index.html) say? What have you tried? What problems do you have with your attempt? Do you have a [mcve] of your attempt to show us? And please take some time to refresh [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 13 '20 at 11:05

1 Answers1

1

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?

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Yeah that work, just tested it now. Thanks for the help. I was close to your solution but didn't know `make_binary_object` could be used in the opposite way. – PepperTiger May 13 '20 at 11:25
  • `make_` does not "make" objects, it _`make`s_ an object _`binary` serializable_ using a [serialization wrapper](https://www.boost.org/doc/libs/1_72_0/libs/serialization/doc/wrappers.html) – sehe May 13 '20 at 11:29