Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to provide some form of serialization for your objects. Here's a very trivial example (that leaves out error checking, type construction etc.)</p> <p>This example shows most primitive type of serialization, but it will suffice to provide an example of what you need to do.</p> <p>When you start dealing with derived classes, it becomes more tricky, as you will need some factory method to properly create the types, based on some sentinel value that you serialize.</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;sstream&gt; using namespace std; class A { public: A() : a_(0) { } A(int a, const string&amp; n) : a_(a), n_(n) { } // a function to write objects to a stream friend ostream&amp; operator&lt;&lt;(ostream&amp; stream, const A&amp; obj); // a function to read objects from a stream friend istream&amp; operator&gt;&gt;(istream&amp; stream, A&amp; obj); private: int a_; string n_; }; ostream&amp; operator&lt;&lt;(ostream&amp; out, const A&amp; obj) { out &lt;&lt; obj.a_ &lt;&lt; ' ' &lt;&lt; obj.n_; return out; } istream&amp; operator&gt;&gt;(istream&amp; in, A&amp; obj) { in &gt;&gt; obj.a_ &gt;&gt; obj.n_; return in; } int main() { A one(1, "one"); A also_one; string buffer; stringstream serializer; // write out your source object to a stream and "store it" // here we simply store it in a string serializer &lt;&lt; one; buffer = serializer.str(); // using the "stored" value, read the stored values // into an (already constructed) object stringstream deserializer(buffer); serializer &gt;&gt; also_one; // verify the output is the same cout &lt;&lt; one &lt;&lt; " is the same as " &lt;&lt; also_one &lt;&lt; "\n"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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