2

boost::serialization ライブラリで、参照を使用し、デフォルトのコンストラクターを使用せずに (ポリモーフィック) オブジェクトを逆シリアル化することは可能ですか?

class Example
{
    int& value;

public:

    Example(int _value): value(_value) {}
    virtual ~Example() {}

    friend class boost::serialization::access;
    template<typename Archive>
    void serialize(Archive & ar, const unsigned int file_version)
    {
        ar & value;
    }
};

class Usage
{
    Example* example;

public:

    Usage(): example(new Example(123)) {}
    ~Usage() { delete example; }

    friend class boost::serialization::access;
    template<typename Archive>
    void serialize(Archive & ar, const unsigned int file_version)
    {
        ar & example;
    }
};

...

// serialize and deserialize object with reference and no default constructor
{
    Usage source;

    std::ostringstream oss;
    boost::archive::text_oarchive oa(oss);
    oa & source;

    Usage target;

    std::istringstream iss(oss.str());
    boost::archive::text_iarchive ia(iss);
    ia & target; // does not compile
}
4

1 に答える 1

5

デフォルト以外の構築可能なオブジェクトについては、項目 Non-Default Constructors hereを参照することをお勧めします。
独自の関数テンプレートload_construct_datasave_construct_data.

于 2011-02-08T21:33:55.617 に答える