1 <!doctype HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
10 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
11 <link rel=
"stylesheet" type=
"text/css" href=
"../../../boost.css">
12 <link rel=
"stylesheet" type=
"text/css" href=
"style.css">
13 <title>Serialization - Private Base Classes
</title>
15 <body link=
"#0000ff" vlink=
"#800080">
16 <table border=
"0" cellpadding=
"7" cellspacing=
"0" width=
"100%" summary=
"header">
18 <td valign=
"top" width=
"300">
19 <h3><a href=
"../../../index.htm"><img height=
"86" width=
"277" alt=
"C++ Boost" src=
"../../../boost.png" border=
"0"></a></h3>
22 <h1 align=
"center">Serialization
</h1>
23 <h2 align=
"center">Private Base Classes
</h2>
28 In many cases, serialization of private or protected base classes present no special problems.
29 This is true for both simple classes and types as well as pointers to those
30 classes and types. That is, the following program compiles and runs exactly as one would expect.
32 /////////
1/////////
2/////////
3/////////
4/////////
5/////////
6/////////
7/////////
8
33 // test_private_base.cpp
35 // (C) Copyright
2009 Eric Moyer - http://www.rrsd.com .
36 // Use, modification and distribution is subject to the Boost Software
37 // License, Version
1.0. (See accompanying file LICENSE_1_0.txt or copy at
38 // http://www.boost.org/LICENSE_1_0.txt)
41 #include
<boost/config.hpp
>
42 #if defined(BOOST_NO_STDC_NAMESPACE)
48 #include
<boost/serialization/access.hpp
>
49 #include
<boost/serialization/base_object.hpp
>
50 #include
<boost/serialization/export.hpp
>
53 friend class boost::serialization::access;
55 template
<class Archive
>
56 void serialize(Archive & ar, const unsigned int version){
57 ar & BOOST_SERIALIZATION_NVP(m_i);
60 bool equals(const Base &rhs) const {
61 return m_i == rhs.m_i;
68 class Derived : private Base {
69 friend class boost::serialization::access;
72 return static_cast
<Base &
>(*this);
74 template
<class Archive
>
75 void serialize(Archive & ar, const unsigned int version){
76 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
79 bool operator==(const Derived &rhs) const {
80 return Base::equals(static_cast
<const Base &
>(rhs));
88 main( int /* argc */, char* /* argv */[] )
90 const char * testfile = boost::archive::tmpnam(NULL);
92 // serialize Derived and Base
95 test_ostream os(testfile);
97 oa << boost::serialization::make_nvp(
"a", a);
100 test_istream is(testfile, TEST_STREAM_FLAGS);
101 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
102 ia
>> boost::serialization::make_nvp(
"a", a1);
104 std::remove(testfile);
109 // serialize Derived and Base
113 test_ostream os(testfile);
114 test_oarchive oa(os);
115 oa << boost::serialization::make_nvp(
"ta", ta);
118 test_istream is(testfile, TEST_STREAM_FLAGS);
119 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
120 ia
>> boost::serialization::make_nvp(
"ta", ta1);
122 std::remove(testfile);
129 Difficulties start to occur when the base class is made polymorphic by the designation
130 of one or more functions as
"virtual". If a class is polymorphic, the library
131 presumes that one will want the ability to serialize a derived class through
132 a pointer to the base class. Included in the macro
134 BOOST_SERIALIZATION_BASE_OBJECT_NVP
136 is code which links derived and base class definitions in tables used to serialize
137 derived classes through pointers to a polymorphinc base class. This code requires
138 the ability to invoke
140 static_cast
<Base &
>(Derived &)
142 which C++ will only permit from within the derived class if the base class is
143 private or protected. The program will fail to compile with an error message
144 indicating invalid cast.
147 code compiler the following alteration must be made:
149 template
<class Archive
>
150 void serialize(Archive & ar, const unsigned int version){
151 //ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
152 ar & boost::serialization::make_nvp(
154 static_cast
<Base &
>(*this)
158 With this change the program will now compile.
160 If we made one of the functions of
<code>Base
></code> <code>virtual
</code>
161 in order to use the
"export" functionality of the serialization library and permit serialization through
162 a pointer the the base class, we'll be disappointed. Without the ability to
163 cast to the base class, we can't use the functionality.
165 <p><i>© Copyright
<a href=
"http://www.rrsd.com">Robert Ramey
</a> 2015.
166 Distributed under the Boost Software License, Version
1.0. (See
167 accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)