Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2008-01-02 01:32:19


Rather than put your program on my debugger, here is the way I anticipated
that the library would be used:

> #include <iostream>
>
> #include <fstream>
>
> #include <boost/serialization/string.hpp>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
>
>
> //----------------------------------------------------------------------------
> using namespace std;
>
> //----------------------------------------------------------------------------
> class A
> {
> private:
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version)
> {
> ar & a;
> ar & b;
> }
>
> public:
> int a;
> int b;
>
> };
>
> //----------------------------------------------------------------------------
> class B
> {
> private:
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int version)
> {
> ar & ms;
> ar & a;
> }
> public:
>
> public:
> B( const string& s ) : ms( s ){}
> void print(){ cout << ms << endl; }
> private:
> B(){} // use by serialization only
> string ms;
> A a;
> };
>
>
> //----------------------------------------------------------------------------
int main()
{
    // Save
   A* pa = new A;
   pa->a = 3;
   pa->b = 5;

   B* pb = new B( "this is test B" );

  {
        std::ofstream ofs( "all.out" );
        boost::archive::text_oarchive oa(ofs);

        oa << pa;
        ob << pb;

        delete pa;
        delete pb;
    }
   A* paa;
   B* pbb
    {
        std::ifstream ifs( "all.out", std::ios::binary );

        boost::archive::text_oarchive ia(ifs);
        ia >> paa;
        ia >> pbb;
    }

    cout << "A: " << paa->a << " " << paa->b << endl;
    pbb->print();

    delete paa;
    delete pbb;
     return 0;
}

Robert Ramey


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net