用基类的指针去转存派生类时除了上一篇
boost::serialization 用基类指针转存派生类(错误多多,一波三折)之外,还有另一种更简单的方法:
用BOOST_CLASS_EXPORT宏。下面我们来分析怎样用BOOST_CLASS_EXPORT来实现
用基类的指针去转存派生类。
用BOOST_CLASS_EXPORT宏。下面我们来分析怎样用BOOST_CLASS_EXPORT来实现
用基类的指针去转存派生类。
首先来看前面实例的一段代码:
void save()
{
std::ofstream ofs("t7.xml");
boost::archive::xml_oarchive oa(ofs);
student_info* sdinfo = new middle_student("wyp", "0099", "1", 15);
oa << BOOST_SERIALIZATION_NVP(sdinfo);//#1
delete sdinfo;
}
当程序执行到#1时就会抛出异常:
boost::archive::archive_exception at memory location 0x0017eb30...
boost文档解释是派生类没有实例化(这里是个人理解。。。。“实例化”到底什么意思也不太理解。。。)。
当我们在#1前面加上注册的代码时
oa.template register_type<middle_student>(NULL);
实际上就相当于“实例化”,看看register_type的实现代码:
template<class T>
const basic_pointer_oserializer *
register_type(const T * = NULL){
const basic_pointer_oserializer & bpos =
boos