Object and multiple inheritance for interfaces
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
Re: Object and multiple inheritance for interfaces
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.
Re: Object and multiple inheritance for interfaces
Multiple inheritance of QObject is not supported, check this post.
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:
Code:
class SomeInterface
{
public:
virtual ~SomeInterface() {}
virtual void operation() = 0;
};
And then implement the interface in various classes:
Code:
class SomeWidget
: public QWidget,
public SomeInterface
{
public:
// SomeInterface
void operation();
};
class SomeObject
: public QObject,
public SomeInterface
{
public:
// 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:
Code:
// 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:
Code:
{
Q_OBJECT
public:
public slots:
// no more "SomeInterface", just slots
void operation();
};
And then we can simply call the methods through QMetaObject:
Code:
// QObject* object
bool ok
= QMetaObject::invokeMethod(object,
"operation");
QMetaObject::invokeMethod() even supports return value and arguments.. Not bad huh?
Thanks bhughes @ #qt @ freenode!
Re: Object and multiple inheritance for interfaces
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?
Re: Object and multiple inheritance for interfaces
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.
Quote:
Originally Posted by jpn
Thanks bhughes @ #qt @ freenode!
Oh yes, Bradley knows all about QObject internals :) Harald likes to abuse the meta-information mechanism as well ;)
Re: Object and multiple inheritance for interfaces
Quote:
Originally Posted by
wysota
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:
Re: Object and multiple inheritance for interfaces
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.
Re: Object and multiple inheritance for interfaces
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 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++.
Re: Object and multiple inheritance for interfaces
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.