title: How do I serialize a class containing pointers to primitives?
body: <p>I am trying to use boost's functionality for serializing pointers to primitives (so that I don't have to de-reference and do a deep store myself). However, I get a pile of errors when I try to do it. Here is a simple example of a class that is supposed to contain <code>save</code> and <code>load</code> methods which write and read the class content from a file. This program does not compile:</p>
  
  <pre><code>#include &lt;boost/archive/text_iarchive.hpp&gt;
  #include &lt;boost/archive/text_oarchive.hpp&gt;
  
  #include &lt;boost/serialization/shared_ptr.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  
  #include &lt;fstream&gt;
  
  class A
  {
  public:
      boost::shared_ptr&lt;int&gt; sp;
      int const * p;
  
      int const&amp; get() {return *p;}
  
      void A::Save(char * const filename);
      static A * const Load(char * const filename);
  
          //////////////////////////////////
          // Boost Serialization:
          //
      private:
          friend class boost::serialization::access;
          template&lt;class Archive&gt;
          void serialize(Archive &amp; ar,const unsigned int file_version)
          {
              ar &amp; p &amp; v;
          }
  };
  
  // save the world to a file:
  void A::Save(char * const filename)
  {
      // create and open a character archive for output
      std::ofstream ofs(filename);
  
      // save data to archive
      {
          boost::archive::text_oarchive oa(ofs);
  
          // write the pointer to file
          oa &lt;&lt; this;
      }
  }
  
  // load world from file
  A * const A::Load(char * const filename)
  {
      A * a;
  
      // create and open an archive for input
      std::ifstream ifs(filename);
  
      boost::archive::text_iarchive ia(ifs);
  
      // read class pointer from archive
      ia &gt;&gt; a;
  
      return a;
  }
  
  int main()
  {
  
  }
  </code></pre>
  
  <p>Note that I am <strong>not</strong> interested in a solution that dereferences the pointer; I want boost to take care of that for me (many of these classes might be pointing to the same underlying object).</p>