我正在尝试使用 序列化一个类boost::serialization
,但是当派生类声明为final
.
#include <fstream>
#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/archive/binary_oarchive.hpp>
class Base {
public:
virtual ~Base() = default;
virtual void Foo() = 0;
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive& archive, const unsigned int version) {}
};
class Derived final: public Base {
public:
void Foo() override {}
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive& archive, const unsigned int version) {
archive & boost::serialization::base_object<Base>(*this);
}
};
int main() {
Derived foo;
std::ofstream output("output");
boost::archive::binary_oarchive archive(output);
archive << foo;
}
error: cannot derive from ‘final’ base ‘Derived’ in derived type ‘boost::detail::is_virtual_base_of_impl<Base, Derived, mpl_::bool_<true> >::boost_type_traits_internal_struct_X’
struct boost_type_traits_internal_struct_X : public Derived, virtual Base
如果final
省略说明符,则没有错误。这个错误的原因是什么以及如何避免它?