PDA

View Full Version : Object and multiple inheritance for interfaces



brcain
16th November 2006, 18:07
Hello,

I'm getting errors due to multiple inheritance of base classes that derive from QObject.

I'm trying to use the Qt meta-object system to enable run-time introspection of interface classes. My goal is to use introspection and dynamic casting to determine if a class implements an interface ... similar to Java style.

A given class may support several interfaces ... so it would use multiple inheritance ... deriving from several interfaces. These interface classes derive from QObject ... necessary for meta-object funcationality.

Cheers,
Ben

brcain
16th November 2006, 18:17
I found the problem. I was declaring a signal with the same name in two of the base classes. The MOC was having trouble resolving which one to actually use.

jpn
16th November 2006, 18:40
Multiple inheritance of QObject is not supported, check this post (http://www.qtcentre.org/forum/p-multiinherit-trouble-template-qlist-post22235/postcount4.html).

Here's a little example what can be done with QMetaObject.
Let's take a look at a classical C++ interface approach first. Here we define an interface:


class SomeInterface
{
public:
virtual ~SomeInterface() {}

virtual void operation() = 0;
};

And then implement the interface in various classes:


class SomeWidget : public QWidget, public SomeInterface
{
public:
SomeWidget(QWidget* parent = 0);

// SomeInterface
void operation();
};

class SomeObject : public QObject, public SomeInterface
{
public:
SomeObject(QWidget* parent = 0);

// SomeInterface
void operation();
};

Ok, so far so good. Then we have a plain QObject pointer in hand which we need then cast to the appropriate type to be able to call SomeInterface methods:


// QObject* object
SomeInterface* some = dynamic_cast<SomeInterface*>(object);
if (some)
{
some->operation();
}

Not very handy is it? Well, Qt at least has a more convenient way for solving such..

We can get away without defining any interface at all by declaring all the functions we had in the interface as slots:


class SomeWidget : public QWidget
{
Q_OBJECT

public:
SomeWidget(QWidget* parent = 0);

public slots:
// no more "SomeInterface", just slots
void operation();
};

And then we can simply call the methods through QMetaObject:


// QObject* object
bool ok = QMetaObject::invokeMethod(object, "operation");

QMetaObject::invokeMethod() even supports return value and arguments.. Not bad huh?

Thanks bhughes @ #qt @ freenode!

brcain
16th November 2006, 19:08
Won't I lose the Q_CLASSINFO, Q_PROPERTY, and Q_ENUMS only using slots? Because they require deriving from QObject.

BTW, I still need multiple inheritance because I'm defining interfaces that will be composed to provide a composite interface.

Concerning slots ... I was thinking that a class had to inherit from QObject to use slots. Does it just require usage of the Q_OBJECT marco?

wysota
16th November 2006, 19:28
QWidget is QObject descendent, so in JPN's example the class actually inherits QObject (through QWidget). You won't loose Q_CLASSINFO and other Qt features as you still inherit QObject and use Q_OBJECT macro which enables you to add the meta information to your class.


Thanks bhughes @ #qt @ freenode!
Oh yes, Bradley knows all about QObject internals :) Harald likes to abuse the meta-information mechanism as well ;)

gfunk
16th November 2006, 19:47
Oh yes, Bradley knows all about QObject internals :)

I saw Bradley at Devdays 2006 in San Jose. He kicks ass! He even had Bjorne Stroustrup enthralled for a while. :cool:

brcain
16th November 2006, 20:05
So, in a nutshell, I can't use Qt meta-object system in conjuction with interface composition.

I still need multiple inheritance because I'm defining interfaces that will be composed to provide a composite interface.

gilbertcane
29th June 2021, 07:28
Allowing multiple inheritence makes the rules about function overloads and virtual dispatch decidedly more tricky, as well as the language implementation around object layouts. These impact language designers/implementors quite a bit, and raise the already high bar to get a language done, stable and adopted.

It is simple to think this way, if class A inherits from multiple classes, then the class A will have the same grandparent class multiple times, this means the code will be complicated and a series of bugs will go unacknowledged. Personally, I think multiple inheritance (http://net-informations.com/faq/oops/inheritancetype.htm) has a bad rap, and that a well done system of trait style composition would be really powerful/useful... but there are a lot of ways that it can be implemented badly, and a lot of reasons it's not a good idea in a language like C++.

d_stranz
29th June 2021, 16:29
Why are you bringing a 15 year old post back from the dead to make a comment that is only vaguely related to the original question? I hope it is not just to shill for the web site you have linked.

You are on the spammer radar watch now.