Boost logo

Boost Users :

From: Steve Sanders (ssanders_at_[hidden])
Date: 2008-07-14 15:39:18


Hi,

 

Below is a basic sample adapted from the documentation at:

http://tinyurl.com/55fr4q

 

For me, with Visual Studio 2008, the deserialization of the base pointer
results in an instance of the base class, not the serialized derived
class. Also, if I add a virtual function to the base class,
deserialization results in an exception (it attempts to instantiate the
base class anyway).

 

I added the change cited here and rebuilt the serialization library,
which did not help:

http://tinyurl.com/5j4e8x

 

Any suggestions would be appreciated.

 

Thanks,

Steve Sanders

 

 

 

#include "stdafx.h"

#include "stdafx.h"

 

#include <sstream>

 

// include headers that implement a archive in simple text format

#include <boost/archive/text_oarchive.hpp>

#include <boost/archive/text_iarchive.hpp>

#include <boost/serialization/export.hpp>

 

 

class base {

public:

    base(){ m_val = 1; }

    void PrintVal( void )

    {

        printf(" Value: %d\n", m_val );

    }

protected:

// virtual void foo(void) = 0;

    friend class boost::serialization::access;

    //...

    // only required when using method 1 below

    // no real serialization required - specify a vestigial one

    template<class Archive>

    void serialize(Archive & ar, const unsigned int file_version){}

 

    int m_val;

};

 

 

class derived : public base {

public:

    derived(){ m_val = 2; }

private:

    void foo(void){}

    friend class boost::serialization::access;

    template<class Archive>

    void serialize(Archive & ar, const unsigned int file_version){

        // method 1 : invoke base class serialization

        boost::serialization::base_object<base>(*this);

        // method 2 : explicitly register base/derived relationship

        boost::serialization::void_cast_register<derived, base>(

            static_cast<derived *>(NULL),

            static_cast<base *>(NULL)

        );

    }

};

 

BOOST_CLASS_EXPORT_GUID(derived, "derived")

 

void test4(){

    //...

    std::stringstream ss;

    boost::archive::text_oarchive oa(ss);

    derived *d = new derived();

    base *b = d;

    b->PrintVal();

    oa & d;

 

    boost::archive::text_iarchive ar(ss);

    b = NULL;

    ar & b;

    b->PrintVal();

}

 

int main() {

    test4();

    return 0;

}

 

 



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