我有protected
我正在处理的基于组件的游戏引擎框架中的构造函数,因此 Component
s 是在 GameSystem
中创建的处理所有权分配并将它们附加到 GameObject
秒。 Component
的 protected 构造函数需要 GameObject&
,因为它的所有者将始终存在于 Component
中的生命周期。
我尝试将序列化应用于 Component
通过覆盖 load_construct_data()
类和 save_construct_data()
功能并让他们成为Component
的 friend ,但我最终会得到这样的结果:
template<class Archive>
inline void load_construct_data(Archive& ar, Component* t, const unsigned int file_version)
{
// Retrieve data from archive required to construct new instance
GameObject gameObject; // This is illegal since GameObject() is protected.
ar >> gameObject;
// Invoke inplace constructor to initialize instance of my_class
::new(t)Component(gameObject);
}
我可以创建一个 GameObject
通过GameSystem
也,或者制作load_construct_data
GameObject
的 friend .我不喜欢友元的扩散,但第一个选择有意义吗?序列化库是否会以某种方式跟踪对象,即使它不是通过其构造函数创建的?请记住,我只需要加载每个对象一次。
还是需要进行重大重新设计?
最佳答案
我已经让 GameObject
有一个公共(public)构造函数来允许访问。解决了这个问题,如果以一种有点笨拙的方式。
关于c++ - boost::序列化和工厂模式设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20091475/