简体   繁体   English

C ++中QObject多重继承和策略/特征设计的问题

[英]Problems with QObject multiple inheritance and policy/traits design in C++

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). 我的应用程序使用基于策略/特征的设计,但是因为我使用Qt,所以它仅通过MI(而不是模板和MI)完成。 Some of these classes are pure virtual and some perform rather critical functions under the hood that the end-user should never touch. 这些类中的一些是纯虚拟的,而某些类在最终用户永远不应接触的幕后执行相当关键的功能。

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. 我的问题是这些类中的某些类需要信号/插槽,因此是从QObject派生的,我可以从该对象继承而来几乎没有问题。 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: 但是,我遇到的问题是,当我想从Qt类派生然后用我的一个或多个特征扩展它时,例如:

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

Here QGLWidget is derived from QObject, but not virtually, causing ambiguity problems. 这里的QGLWidget是从QObject派生的,但实际上不是从QObject派生的,从而引起歧义问题。

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. 我考虑过一种桥接模式,在该模式中,我制作了Sy_saveable纯虚拟对象,然后从其中导出包含实际实现的Sy_saveable_imp Then use that for my Sy_abstractGLViewport via aggregation. 然后通过聚合将其用于我的Sy_abstractGLViewport

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. 在我看来,这不是很专业,因为该应用程序是基于插件的,对于我将来的插件编写者来说,将所有接口方法“连接”到聚合实例上有点像PITA。 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? 还是不需要MI但给我同样灵活性的模式? This my personal hobby project, I don't mind doing a lot of refactoring - I want to do it right . 这是我个人的业余爱好项目,我不介意进行大量重构-我想做正确的事

You can't inherit from multiples clasess that inherit QObject. 您不能从继承QObject的多重句柄继承。

Try using composition instead of inheritance. 尝试使用合成而不是继承。 See composition over inheritance . 请参见组成而不是继承

You can also try using Q3Signal class. 您也可以尝试使用Q3Signal类。 Although it belongs to old QT3 according to QT doc : 尽管根据QT doc,它属于旧的QT3:

The Q3Signal class can be used to send signals for classes that don't inherit QObject. Q3Signal类可用于为不继承QObject的类发送信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM