Ticket #4681: c.cc

File c.cc, 1.4 KB (added by mark.r.buda@…, 12 years ago)

demonstration of the problem with serializing protected classes

Line 
1#include <boost/archive/binary_oarchive.hpp>
2#include <boost/serialization/export.hpp>
3#include <fstream>
4
5class base {
6public:
7 virtual ~base() {}
8private:
9 friend class boost::serialization::access;
10 template<class Archive>
11 void serialize(Archive &ar, const unsigned int version)
12 {
13 }
14};
15
16class wrapper {
17protected:
18 class derived : public base {
19 private:
20 friend class boost::serialization::access;
21 template<class Archive>
22 void serialize(Archive &ar, const unsigned int version)
23 {
24 boost::serialization::base_object<base>(*this);
25 }
26 };
27public:
28 wrapper() : b(new derived()) {}
29private:
30 base *b;
31 friend class boost::serialization::access;
32 template<class Archive>
33 void serialize(Archive &ar, const unsigned int version)
34 {
35 ar & b;
36 }
37 // uncommenting this line and the line below appear to work, so i'll write a macro for those.
38 //static ::boost::archive::detail::guid_initializer< wrapper::derived > const & guid_initializer;
39};
40
41//::boost::archive::detail::guid_initializer< wrapper::derived > const & wrapper::guid_initializer = ::boost::serialization::singleton< ::boost::archive::detail::guid_initializer< wrapper::derived > >::get_mutable_instance().export_guid("derived");
42
43BOOST_CLASS_EXPORT_GUID(wrapper::derived, "wrapper::derived");
44
45main()
46{
47 std::ofstream s("/tmp/foo.out");
48 boost::archive::binary_oarchive a(s);
49
50 wrapper o = wrapper();
51
52 a & o;
53}