samedi 20 avril 2019

How to deserialize data of different types

Saying that I have two kinds of classes: class A and class B:

class A{};
class B{};

I have a file named test.srz, containing the data serialized from an object A or an object B. Now I want to deserialize it, here is the code:

#include <iostream>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

int main() {
    std::ifstream ifile("test.srz");
    boost::archive::text_iarchive iTextArchive(ifile);
    ??? obj;
    iTextArchive >> obj;
}

My question is: How do I know the type of obj? In this case, it could be A or B. If I am able to get its type, it has already been deserialized, right?

Aucun commentaire:

Enregistrer un commentaire