Boost序列化:默认构造函数必须是公共的吗?
Boost Serialization : Does the default constructor have to be public?
只是一些似乎没有记录在任何地方的信息。有人知道吗?因为我想把它设为私有,希望构造函数可以从被声明为友元的boost::serialization::access
调用。
测试示例。考虑到这可以工作,我认为这是一个特性,如果未来的版本不允许访问授予机制授予私有默认构造函数的访问权限,我会感到不安。
#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++中序列化结构数据
- 序列化,没有库的整数,得到奇怪的结果
- 如何知道QDataStream不能反序列化某些内容
- 如何使用Python从C++中读取谷物序列化数据
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 自定义对象的dlib序列化在gcc中失败
- C++boost序列化多态性问题
- 增强基于 XML class_id的反序列化
- 提升反序列化对象具有 nan 或 -nan 值
- 在 cpp 中的平面缓冲区中序列化对象
- 每次进行继承时都需要提升::序列化::base_object吗?
- 如何使用带有非默认构造函数的指针来提升序列化
- 是否可以反序列化(从原始内存块)没有默认构造函数的对象?
- 非默认可构造和 std::引用包装器替代项的序列化
- Boost::序列化 - 如何序列化需要数据分配但没有默认构造函数的数据结构
- 反序列化没有默认构造函数的 STL 容器类型
- 如何更改Boost.Serialization中的默认枚举序列化
- Boost序列化:默认构造函数必须是公共的吗?
- C++和提升 XML 存档(序列化):是否可以指定元素的标记名称?(默认值:"item" )