如何覆盖 boost::serialize 获取指向对象的 POINTER 时发生的情况
嘿,所以我明白,只要您为其指向的对象定义了序列化函数,boost 就会自动序列化指针,但是
如果我想编写一个采用 myClass 指针的 boost 序列化函数,我该怎么办?
我不希望 boost 执行保存所指向的对象然后恢复指针以指向该对象的默认操作。我想要推动做一些不同的事情。
Hey so I understand boost serializes pointers automatically as long as you've defined the serialization function for the object it's pointing to, but
what do I do if I want to write a boost serialization function that takes a myClass pointer
?
I don't want the boost to do the default action of saving the object that's pointed to and then restoring the pointer to point to that. I want the boost to do something different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要与特殊类的普通指针序列化不同的行为,您有两个选择:
也许您可以调整包含 myClass 指针的所有结构/类的 serialize() 方法,以实现您希望的行为想。但是,如果您周围有很多这样的指针,那么这将不是一个选择。另一种可能性可能是使用 http 中描述的免费函数://www.boost.org/doc/libs/1_47_0/libs/serialization/doc/index.html(为 myClass 指针编写)。
另一个选项仅在您使用不超过一种存档类型(例如二进制存档)时才有效。您可以从存档类派生并为方法 save(分别为和 load)添加重载。
If you need a behaviour that is different from the normal pointer serialization for your special class, you have two options:
Maybe you can adjust the serialize() methods of all structs/classes that contain your myClass pointer in order to achieve the behaviour that you want. However, if you have many such pointers around, this won't be an option. Another possibility might be to use a free function as described in http://www.boost.org/doc/libs/1_47_0/libs/serialization/doc/index.html (written for the myClass pointer).
The other option only works if you are using no more than one archive type (e.g. the binary archive). You can derive from the archive classes and add an overload for the method save (and load, respectively).