QObject多重继承和策略/ traits设计在C ++中的问题 [英] Problems with QObject multiple inheritance and policy/traits design in C++

查看:376
本文介绍了QObject多重继承和策略/ traits设计在C ++中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在业余时间正在建立一个相当大的插件驱动的应用程序,并且遇到了一个显示停止设计缺陷。我的应用程序使用基于策略/ traits的设计,但因为我使用Qt它只是通过MI(而不是模板和MI)。这些类中的一些是纯虚拟的,并且一些在最终用户不应该触摸的情况下执行相当关键的功能。

I'm building a fairly large plugin-driven app in my spare time, and have come across a show stopping design flaw. My app uses policy/traits based design, but because I use Qt it is done just through MI (rather than templates and MI). Some of these classes are pure virtual and some perform rather critical functions under the hood that the end-user should never touch.

我的问题是这些类中的一些需要信号/ slots,因此从QObject派生,没有问题,我可以只是虚拟地继承它。但是,我的问题是,当我想从一个Qt类派生,然后扩展它一个或多个我的特质,例如:

My problem is that some of these classes require signals/slots and therefore derive from QObject, no problem I can just virtually inherit from it. However, the problem I have is when I want derive from a Qt class and then extend it with one or more of my traits, eg:

class Sy_abstractGLViewport : public QGLWidget, public Sy_saveable, public Sy_abstractObject
{
    ...
}

这里的QGLWidget派生自QObject,但不是虚拟的,导致歧义问题。

Here QGLWidget is derived from QObject, but not virtually, causing ambiguity problems.

我考虑了一个桥模式例如我的 Sy_saveable pure virtual,然后从它导出一个包含实际实现的 Sy_saveable_imp 。然后通过聚合使用它为我的 Sy_abstractGLViewport

I've considered a Bridge pattern where I make for example my Sy_saveable pure virtual and then derive a Sy_saveable_imp from it that contains the actual implementation. Then use that for my Sy_abstractGLViewport via aggregation.

这对我来说似乎相当不专业, ,它是一个PITA为我的未来插件编写者连接所有的接口方法到聚合实例。我甚至不能通过宏自动化它,因为最终用户可能想要重写一个方法。

This to me seems rather unprofessional, because the app is plugin based, it's a bit of a PITA for my future plugin writers to 'hook up' all the interface methods to the aggregated instance. I can't even automate it through macros because the end-user may want to override a method.

有没有人一个模式来解决这个问题?或者一种不需要心肌梗死但又给我相同灵活性的模式?这是我个人的爱好项目,我不介意做很多重构 - 我想这样做

Has anyone a pattern to solve this issue? Or a pattern that doesn't require MI but gives me the same flexibility? This my personal hobby project, I don't mind doing a lot of refactoring - I want to do it right.

推荐答案

不能继承继承QObject的多个clasess。

You can't inherit from multiples clasess that inherit QObject.

尝试使用composition而不是继承。请参见继承构成

Try using composition instead of inheritance. See composition over inheritance.

您也可以尝试使用 Q3Signal 类。虽然它根据QT doc属于旧的QT3:

You can also try using Q3Signal class. Although it belongs to old QT3 according to QT doc :


Q3Signal类可以用于发送不是$ b的类的信号$ b继承QObject。

The Q3Signal class can be used to send signals for classes that don't inherit QObject.

这篇关于QObject多重继承和策略/ traits设计在C ++中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆