c++ - boost 序列化 : Does the default constructor have to be public?

标签 c++ serialization boost

只是一点点信息,似乎在任何地方都没有记录。有人知道吗?因为我想将其设为私有(private),希望从声明为友元的 boost::serialization::access 调用构造函数。

最佳答案

测试示例。鉴于这是有效的,我假设它是一个功能,如果 future 的版本不允许访问授权机制向私有(private)默认构造函数授予访问权限,我会感到不安。

#include <boost/serialization/access.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
#include <iostream>

struct Colour {
    double colour[4];
    boost::shared_ptr<Colour> alt;
    static boost::shared_ptr<Colour> test() {
       return boost::shared_ptr<Colour>(new Colour);
    }
private:
    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int /*file_version*/) {
        ar & colour;
    }

    Colour() {
        std::cout << "Getting called" << std::endl;
    }
};

int main() {
    boost::shared_ptr<Colour> c = Colour::test();
    c->alt = Colour::test();

    std::cout << "Created" << std::endl;

    std::stringstream str;
    boost::archive::text_oarchive oa(str);
    oa & c;

    std::cout << "Saved" << std::endl;

    c.reset();

    boost::archive::text_iarchive ia(str);
    ia & c;

    std::cout << "Restored" << std::endl;
}

(有趣的是,它似乎默认构造一个然后在我的系统上复制构造另一个)。

关于c++ - boost 序列化 : Does the default constructor have to be public?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7123613/

相关文章:

c++ - 在 Linux 中从单独的单线程进程生成多线程进程的问题

serialization - Lua中的反序列化

powershell - 反序列化对象类型问题 - 特别是 Powershell 5 类和 Import-CliXml

android - ios低级序列化

c++ - 使用boost的字节序转换

c++ - 无法使用 CMake 构建 STXXL

c++ - 从未达到打开枚举后的保护代码

c++ - 我如何在 boost::spirit::qi 中解析带有换行符的列表?

C++使用atof将字符串转换为double

c++ - boost::this_thread::sleep_用于使整个程序休眠