Qt 我应该从 QDataStream 派生吗?

发布于 2024-08-25 08:10:27 字数 330 浏览 3 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2024-09-01 08:10:27

这很快就会失控。更好的方法是在 QDataStream 和您的类之间定义 operator<<operator>>。更干净的方法可能是简单地在您的类上使用序列化方法来读取/写入 QDataStream (这样您就可以调用例如 obj->serialize(myStream) )。

That would get out of control very quickly. A better approach is to define operator<< and operator>> between QDataStream and your class. Even cleaner might be to simply have serialization methods on your classes that read/write to a QDataStream (so that you would call for example, obj->serialize(myStream)).

幼儿园老大 2024-09-01 08:10:27

我当前处理此问题的方法如下:

我为每个需要序列化的对象创建一个序列化器类,而不是从 QDataStream 派生。

例如,如果我有:

class MyOwnClass

那么我会创建一个名为:的类:

class MyOwnClassSerializer
{
public:
  static QByteArray const serialize(MyOwnClass const&);
};

我没有

QByteArray serialize() const;

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:

class MyOwnClass

I would then create a class called:

class MyOwnClassSerializer
{
public:
  static QByteArray const serialize(MyOwnClass const&);
};

The reason why I didn't add a

QByteArray serialize() const;

to class MyOwnClass is so that people can use MyOwnClass without depending on Qt. Also not all MyOwnClass clients are interested in serializing the class.

Any comments would be appreciated! =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文