gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:17:16 24 4
gpt4 key购买 nike

只是一点点信息,似乎在任何地方都没有记录。有人知道吗?因为我想将其设为私有(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com