I want to serialize a derived class object through boost::serialization
library. To test whether both the base and the derived class object have been
serialized properly, I print message to the console when the serialize
function is called. When the following code is compiled and runned, the
message of serialize function for the base class object is not shown. Thus,
the base class object is not serialized properly. Why the serialization is not
done on the base class object?
#include <cstdlib>
#include <iostream>
#include <fstream>
#include
#include
#include
#include
#include
using namespace std;
using namespace boost::archive;
using boost::shared_ptr;
class Base {
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version);
protected:
int a;
public:
Base():a(0){}
virtual ~Base(){}
int geta() const {return a;}
void seta(int aa) {a = aa;}
};
template<class Archive>
void Base::serialize(Archive & ar,const unsigned int version)
{
cout<<"serializing base class object"< (
static_cast(NULL),
static_cast(NULL)
);
cout<<"serializing derived class object"<seta(4);
Base * p = ptr.get();
Derived * pp = dynamic_cast(p);
assert(pp);
pp->setb(4);
ofstream out("test.dat");
text_oarchive oa(out);
oa << p;
return EXIT_SUCCESS;
}