#include "BaseClass.h" #include "InstantiableClass.h" // Assert the values are as expected. void test(BaseClass& object) { assert(object.getId() == 10); assert(dynamic_cast(object).getCom() == 20); } // Construct the object. std::unique_ptr getObject(int which) { switch (which) { case 1: return std::unique_ptr(new InstantiableClass); default: throw std::exception(); } } // Serialize the object. void serialize(std::string filename, BaseClass& object) { std::ofstream file(filename.c_str(), std::ios::trunc); boost::archive::binary_oarchive archive(file); archive << object; file.close(); } // Serialize the object. void deserialize(std::string filename, BaseClass& object) { std::ifstream file(filename.c_str()); boost::archive::binary_iarchive archive(file); archive >> object; file.close(); } // Main function. int main(int argc,char *argv[]) { // Serialize the object (client). std::cout << "Serializing..." << std::endl; InstantiableClass source(20); test(source); serialize("binary_test_file.dat", source); // De-serialise the object (server). std::cout << "De-serializing..." << std::endl; std::unique_ptr copy = getObject(1); deserialize("binary_test_file.dat", *copy); test(*copy); }