Results 1 to 9 of 9

Thread: Object and multiple inheritance for interfaces

  1. #1
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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

  2. #2
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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:
    Qt Code:
    1. class SomeInterface
    2. {
    3. public:
    4. virtual ~SomeInterface() {}
    5.  
    6. virtual void operation() = 0;
    7. };
    To copy to clipboard, switch view to plain text mode 
    And then implement the interface in various classes:
    Qt Code:
    1. class SomeWidget : public QWidget, public SomeInterface
    2. {
    3. public:
    4. SomeWidget(QWidget* parent = 0);
    5.  
    6. // SomeInterface
    7. void operation();
    8. };
    9.  
    10. class SomeObject : public QObject, public SomeInterface
    11. {
    12. public:
    13. SomeObject(QWidget* parent = 0);
    14.  
    15. // SomeInterface
    16. void operation();
    17. };
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. // QObject* object
    2. SomeInterface* some = dynamic_cast<SomeInterface*>(object);
    3. if (some)
    4. {
    5. some->operation();
    6. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. class SomeWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. SomeWidget(QWidget* parent = 0);
    7.  
    8. public slots:
    9. // no more "SomeInterface", just slots
    10. void operation();
    11. };
    To copy to clipboard, switch view to plain text mode 
    And then we can simply call the methods through QMetaObject:
    Qt Code:
    1. // QObject* object
    2. bool ok = QMetaObject::invokeMethod(object, "operation");
    To copy to clipboard, switch view to plain text mode 
    QMetaObject::invokeMethod() even supports return value and arguments.. Not bad huh?

    Thanks bhughes @ #qt @ freenode!
    J-P Nurmi

  4. #4
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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

  6. #6
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Object and multiple inheritance for interfaces

    Quote Originally Posted by wysota View Post
    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.
    Software Engineer



  7. #7
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default 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.

  8. #8
    Join Date
    Jun 2021
    Posts
    1
    Qt products
    Platforms
    Windows Android

    Default 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++.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,021
    Thanks
    280
    Thanked 818 Times in 807 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. how to corss compile for windows in Linux
    By safknw in forum Qt Programming
    Replies: 24
    Last Post: 13th May 2006, 05:23
  2. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.