Qt 我应该从 QDataStream 派生吗?
我目前正在使用 QDataStream 来序列化我的类。我有很多自己的类,我经常序列化。我应该派生 QDataStream 来创建我自己的 DataStream 类吗?或者还有比这更好的模式吗?请注意,我们的许多项目都使用这些自定义类,因此这样做可能会使编码变得更容易。
表达这个问题的另一种方式是:当框架为您提供序列化类时,您如何处理序列化您自己的自定义类型类,这样您就不必每次都记住如何序列化它们(增强可用性)并且还可以遵循最佳软件工程实践(以下模式)。
I'm currently using QDataStream to serialize my classes. I have quite a few number of my own classes that I serialize often. Should I derive QDataStream to create my own DataStream class? Or is there a better pattern than this? Note that these custom classes are used by many of our projects, so maybe doing so will make coding easier.
Another way to phrase this question is: when a framework provides you with a serialization class, how do you handle serializing your own custom-type classes such that you don't have to remember how to serialize them everytime (enhance usability) and also following best software-engineering practices (following patterns).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很快就会失控。更好的方法是在
QDataStream
和您的类之间定义operator<<
和operator>>
。更干净的方法可能是简单地在您的类上使用序列化方法来读取/写入 QDataStream (这样您就可以调用例如obj->serialize(myStream)
)。That would get out of control very quickly. A better approach is to define
operator<<
andoperator>>
betweenQDataStream
and your class. Even cleaner might be to simply have serialization methods on your classes that read/write to aQDataStream
(so that you would call for example,obj->serialize(myStream)
).我当前处理此问题的方法如下:
我为每个需要序列化的对象创建一个序列化器类,而不是从 QDataStream 派生。
例如,如果我有:
那么我会创建一个名为:的类:
我没有
向
class MyOwnClass
添加 a 的原因是这样人们可以使用MyOwnClass
而无需依赖在 Qt 上。此外,并非所有MyOwnClass
客户端都对序列化类感兴趣。任何评论将不胜感激! =)
The current way that I'm handling this is as follow:
Rather than deriving from QDataStream, I create a serializer class for each object I need to serialize.
For example, if I have:
I would then create a class called:
The reason why I didn't add a
to
class MyOwnClass
is so that people can useMyOwnClass
without depending on Qt. Also not allMyOwnClass
clients are interested in serializing the class.Any comments would be appreciated! =)