PDA

View Full Version : Multiple inheritance with QObject



victor.fernandez
22nd April 2010, 16:34
Hi! I have some classes defining a tree structure.


class BaseNode : public QObject;
class SequentialNode : public BaseNode;
class ParallelNode : public BaseNode;

BaseNode has a couple of signals and two slots, which are inherited by SequentialNode and ParallelNode. This is a general implementation of an algorithm.

Now I would like to be able to subclass all this classes in order to make a custom implementation. So I'd do:


class CustomBaseNode : public BaseNode;
class CustomSequentialNode; // inherits... what?
class CustomParallelNode; // inherits... what?

I would like to add one signal to CustomBaseNode, which is a reimplementation of BaseNode. Well, no problem here yet.

CustomBaseNode should be inherited by CustomSequentialNode and CustomParallelNode following the same tree structure, so they should be subclasses of CustomBaseNode. Let's say:


class CustomBaseNode : public BaseNode;
class CustomSequentialNode : public CustomBaseNode;
class CustomParallelNode : public CustomBaseNode;

But this way CustomSequentialNode is not a subclass of SequentialNode and doesn't have its funcionality. On the other hand, if I make it inherit SequentialNode it doesn't inherit the signal and functionality from CustomBaseNode.

I could try to use multiple inheritance and make CustomSequentialNode inherit both CustomBaseNode and SequentialNode.


class CustomBaseNode : public BaseNode;
class CustomSequentialNode : public CustomBaseNode, public SequentialNode;
class CustomParallelNode : public CustomBaseNode, public ParallelNode;

However, for CustomSequentialNode, both CustomBaseNode and SequentialNode are subclasses ofQObject. As far as I've understand from the Qt docs, QObject doesn't support multiple inheritance from more than one QObject.

In the end, CustomSequentialNode would be inheriting BaseNode and QObject twice (once from SequentialNode - BaseNode - QObject and again from CustomBaseNode - BaseNode - QObject). I want to use signals, so I need QObject.

Is there some design pattern or standard way to solve this issue?

Thanks.

wysota
22nd April 2010, 16:40
This won't work this way. Use the "bridge" design pattern.