Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Found a resolution. I had to export the derived class with the statement:</p> <pre><code>BOOST_CLASS_EXPORT(derived); </code></pre> <p>Posting something that works with some corrections.</p> <pre><code>using namespace std; class base { public: int data1; friend class boost::serialization::access; template&lt;typename Archive&gt; void serialize(Archive &amp; ar, const unsigned int file_version) { ar &amp; data1; } public: base() {}; base(int _d) : data1(_d) {} virtual void foo() const {std::cout &lt;&lt; "base" &lt;&lt; std::endl;} }; class derived : public base { public: int data2; friend class boost::serialization::access; template&lt;typename Archive&gt; void serialize(Archive &amp; ar, const unsigned int file_version) { ar &amp; boost::serialization::base_object&lt;base&gt;(*this); ar &amp; data2; } public: derived() {}; derived(int _b, int _d) : base(_b), data2(_d) {} virtual void foo() const {std::cout &lt;&lt; "derived" &lt;&lt; std::endl;} }; BOOST_CLASS_EXPORT(derived); int main(int argc, char *argv[]) { // client // Assign to base type std::unique_ptr&lt;const base&gt; b1(new derived(1, 2)); std::ostringstream oss; boost::archive::text_oarchive oa(oss); oa &amp; b1.get(); // server // Retrieve derived type from base std::unique_ptr&lt;base&gt; b2; std::istringstream iss(oss.str()); boost::archive::text_iarchive ia(iss); { base *temp; ia &amp; temp; b2.reset(temp); } cout &lt;&lt; b2-&gt;data1 &lt;&lt; endl; cout &lt;&lt; (dynamic_cast&lt;derived*&gt;(b2.get()))-&gt;data2 &lt;&lt; endl; return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload