boost::serialization 用基类指针转存派生类(错误多多,一波三折)

原创 2014年05月03日 14:56:54
boost::serialization 也支持c++的多态,这样我们就可以通过使用基类的指针来转存派生类,
我们接着上一篇( 

boost::serialization(2)序列化基类

)的例子来看:

基类和派生类的代码如下:

class student_info
{
public:
	student_info() {}
	virtual ~student_info() {}
	student_info(const std::string& sn, const std::string& snm, const std::string& sg)
		: name_(sn), number_(snm), grade_(sg)
	{
	}

	virtual void print_info() const
	{
		std::cout << name_ << " " << number_ << " " << grade_ << std::endl;
	}

private:
	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & BOOST_SERIALIZATION_NVP(name_);
		ar & BOOST_SERIALIZATION_NVP(number_);
		ar & BOOST_SERIALIZATION_NVP(grade_);
	}

private:
	std::string name_;
	std::string number_;
	std::string grade_;
};

class middle_student : public student_info
{
public:
	middle_student() {}
	virtual ~middle_student() {}
	middle_student(const std::string& sn, const std::string& snm, const std::string& sg, int age)
		: student_info(sn, snm, sg), age_(age)
	{

	}

	virtual void print_info()
	{
		student_info::print_info();
		std::cout << age_ << std::endl;
	}

private:
	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & boost::serialization::base_object<student_info>(*this);
		ar & BOOST_SERIALIZATION_NVP(age_);
	}

private:
	int age_;
};
在派生类中使用了基类的基类的序列化: ar & boost::serialization::base_object<student_info>(*this);
下面我们来看怎么使用基类的指针转存派生类:
save的代码:
void save()
{
	std::ofstream ofs("t7.xml");
	boost::archive::xml_oarchive oa(ofs);
	student_info* sdinfo = new middle_student("wyp", "0099", "1", 15);//#1
	oa << BOOST_SERIALIZATION_NVP(sdinfo);//#2
	std::cout << "xxxx" << std::endl;
	delete sdinfo;
}
#1:用一个基类的指针指向了一个用new申请的派生类的指针,很简单,都知道这就是c++的多态。
#2:这个代码和以前的一样,还是用一个宏来包装指针。


load的代码:

void load()
{
	std::ifstream ifs("t7.xml");
	boost::archive::xml_iarchive ia(ifs);
	student_info* sdinfo = NULL;//#1
	ia >> BOOST_SERIALIZATION_NVP(sdinfo);//#2
	middle_student* mds = dynamic_cast<middle_student*>(sdinfo);//#3
	mds->print_info();
}
#1:基类的指针
#2:load的时候也需要宏来包装
#3:这个大家都熟悉


测试代码:

void fun()
{
	save();
	load();
}
编译运行!。。。。。。。
结果抛出异常:boost::archive::archive_exception at memory location 0x0017eb30...
google了一下,下面链接给出了一个解决方法
http://stackoverflow.com/questions/1332602/how-to-serialize-derived-template-classes-with-boost-serialize
大概分为3个步骤:
步骤1:BOOST_SERIALIZATION_ASSUME_ABSTRACT(className),用这个宏来告诉boost className是一个抽象类
步骤2:在save操作中注册派生类:oa.template register_type<middle_student>(NULL),
       一定要在oa << BOOST_SERIALIZATION_NVP(sdinfo)之前注册。
步骤3:在load操作中注册派生了:ia.template register_type<middle_student>(NULL)
       一定要在ia >> BOOST_SERIALIZATION_NVP(sdinfo)之前注册。
修改后的代码如下:
void save()
{
	std::ofstream ofs("t7.xml");
	boost::archive::xml_oarchive oa(ofs);
	oa.template register_type<middle_student>(NULL);
	student_info* sdinfo = new middle_student("wyp", "0099", "1", 15);
	oa << BOOST_SERIALIZATION_NVP(sdinfo);
	delete sdinfo;
}

void load()
{
	std::ifstream ifs("t7.xml");
	boost::archive::xml_iarchive ia(ifs);
	ia.template register_type<middle_student>(NULL);
	student_info* sdinfo = NULL;
	ia >> BOOST_SERIALIZATION_NVP(sdinfo);
	middle_student* mds = dynamic_cast<middle_student*>(sdinfo);
	mds->print_info();
}
好这下应改没有异常了吧。

结果编译就出错了!!!


错误在这个函数里面

 // Anything not an attribute and not a name-value pair is an
    // error and should be trapped here.
    template<class T>
    void save_override(T & t, BOOST_PFTO int)
    {
        // If your program fails to compile here, its most likely due to
        // not specifying an nvp wrapper around the variable to
        // be serialized.
        BOOST_MPL_ASSERT((serialization::is_wrapper< T >));
        this->detail_common_oarchive::save_override(t, 0);
    }
看注释就知道了,序列化时存在有些数据没有包装,就是没有用那个宏。

就仔细看一下序列化的代码发现这段代码中有一个没用宏来包装:

private:
	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & boost::serialization::base_object<student_info>(*this);//here!!!!!!!!!!!
		ar & BOOST_SERIALIZATION_NVP(age_);
	}
在调用基类的序列化时没用宏包装

修改如下:

private:
	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned int version)
	{
		//ar & boost::serialization::base_object<student_info>(*this);
		ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(student_info);
		ar & BOOST_SERIALIZATION_NVP(age_);
	}
编译运行ok!

运行结果如下 t7.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="10">
<sdinfo class_id="0" tracking_level="1" version="0" object_id="_0">
<student_info class_id="1" tracking_level="1" version="0" object_id="_1">
<name_>wyp</name_>
<number_>0099</number_>
<grade_>1</grade_>
</student_info>
<age_>15</age_>
</sdinfo>
</boost_serialization>

完整代码如下

#include <fstream>
#include <iostream>
#include <algorithm>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/base_object.hpp>

class student_info
{
public:
	student_info() {}
	virtual ~student_info() {}
	student_info(const std::string& sn, const std::string& snm, const std::string& sg)
		: name_(sn), number_(snm), grade_(sg)
	{
	}

	virtual void print_info() const
	{
		std::cout << name_ << " " << number_ << " " << grade_ << std::endl;
	}

private:
	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & BOOST_SERIALIZATION_NVP(name_);
		ar & BOOST_SERIALIZATION_NVP(number_);
		ar & BOOST_SERIALIZATION_NVP(grade_);
	}

private:
	std::string name_;
	std::string number_;
	std::string grade_;
};

BOOST_SERIALIZATION_ASSUME_ABSTRACT(student_info)

class middle_student : public student_info
{
public:
	middle_student() {}
	virtual ~middle_student() {}
	middle_student(const std::string& sn, const std::string& snm, const std::string& sg, int age)
		: student_info(sn, snm, sg), age_(age)
	{

	}

	virtual void print_info()
	{
		student_info::print_info();
		std::cout << age_ << std::endl;
	}

private:
	friend class boost::serialization::access;
	template<typename Archive>
	void serialize(Archive& ar, const unsigned int version)
	{
		ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(student_info);
		ar & BOOST_SERIALIZATION_NVP(age_);
	}

private:
	int age_;
};

void save()
{
	std::ofstream ofs("t7.xml");
	boost::archive::xml_oarchive oa(ofs);
	oa.template register_type<middle_student>(NULL);
	student_info* sdinfo = new middle_student("wyp", "0099", "1", 15);
	oa << BOOST_SERIALIZATION_NVP(sdinfo);
	delete sdinfo;
}

void load()
{
	std::ifstream ifs("t7.xml");
	boost::archive::xml_iarchive ia(ifs);
	ia.template register_type<middle_student>(NULL);
	student_info* sdinfo = NULL;
	ia >> BOOST_SERIALIZATION_NVP(sdinfo);
	middle_student* mds = dynamic_cast<middle_student*>(sdinfo);
	mds->print_info();
}

void fun()
{
	save();
	load();
}







相关文章推荐

《虚函数——用基类指针访问派生类中的成员函数》

/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称: 《...

面向对象--多继承&派生类对象内存布局分析&各基类指针所指向的位置分析

如果C同时派生了基类A和基类B,那么C的对象的内存是如何分布的呢?基类指针pa和基类指针pb对这块派生类C对象的内存所存储的内容又做和解析?或者说pa和pb指向的地址是否是C对象的首地址呢? 1.继承...
  • ordeder
  • ordeder
  • 2014年05月10日 14:21
  • 2859

为什么基类指针和引用可以指向派生类对象,但是反过来不行?

基类指针和引用BaseClass *pbase = NULL; DerivedClass dclass; pbase = & dclass; 基类指针和引用可以指向派生类对象,但是无法使用不存在...

基类指针指向派生类对象

成员函数一般化三个结论 #include using std::cout; using std::endl; class Employee { public:         void Work(...

基类指针指向派生类对象和隐式转换

1、调用虚函数 调用的是派生类的函数

基类指针指向派生类

#include class A { public: void a1(){ std::cout

关于指向派生类的基类指针或引用

基类指针或引用只能用于调用基类方法,不能使用基类指针或引用指向派生类方法。...

基类指针指向派生类是安全的,而派生类指向基类则是不安全的

基类指针指向派生类是安全的,而派生类指向基类则是不安全的

怎样用boost::serialization去序列化派生模板类(续)

在 怎样用boost::serialization去序列化派生模板类这篇文章中,介绍了序列化派生类模板类, 在写测试用例时一直出现编译错误,调了很久也没跳出来,今天偶然试了一下...居然调了出来...

怎样用boost::serialization去序列化派生模板类

本篇是boost::serialization 用基类指针转存派生类(错误多多,一波三折)的姊妹篇,这里只不过做一个总结。 先来看一个基类 class base_class { public: ba...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:boost::serialization 用基类指针转存派生类(错误多多,一波三折)
举报原因:
原因补充:

(最多只允许输入30个字)