August 5, 2011

genC genC
Lab Rat
14 posts

Qt Meta-Object System and virtual functions with multiple inheritance - unexpected behaviour

 

Hello everyone,

I have a weird problem regarding virtual functions with multiple inheritance and meta-object.
I have a class, MainClass, that inherits QObject, and an abstract class AbstractClass that doesn’t inherit anything. Then, I have a class MyClass that inherits both MainClass and AbstractClass. AbstractClass declares a pure virtual function isAbstractClass(). This function is defined in MyClass.
I created an instance of MyClass via metaObject.newInstance(), and when I call isAbstractClass(), it doesn’t give me the expected result.
I ran with debugging (see breakpoint location in code below), and stepped into the function. It just jumped to MyClass::metaObject() in moc_MyClass.cpp instead of MyClass::isAbstractClass() in MyClass.cpp…

If I don’t use multiple inheritance (having AbstractClass inherit from MainClass, and MyClass from AbstractClass for instance), it just runs fine.

Why is it behaving this way? Is it an moc limitation? Are there any solutions? Any help will be greatly appreciated.

Thanks!

MainClass.h:

  1. #ifndef MAINCLASS_H
  2. #define MAINCLASS_H
  3.  
  4. #include <QObject>
  5. #include <QMetaObject>
  6. #include <QVariant>
  7.  
  8. class MainClass : public QObject
  9. {
  10.     Q_OBJECT
  11.  
  12. public:
  13.     MainClass();
  14.  
  15.     virtual ~MainClass();
  16. };
  17.  
  18. Q_DECLARE_METATYPE(MainClass*)
  19.  
  20. #endif // MAINCLASS_H

AbstractClass.h:

  1. #ifndef ABSTRACTCLASS_H
  2. #define ABSTRACTCLASS_H
  3.  
  4. class AbstractClass
  5. {
  6. public:
  7.     AbstractClass();
  8.  
  9.     virtual bool isAbstractClass() const = 0;
  10. };
  11.  
  12. #endif // ABSTRACTCLASS_H

MyClass.h:

  1. #ifndef MYCLASS_H
  2. #define MYCLASS_H
  3.  
  4. #include "MainClass.h"
  5. #include "AbstractClass.h"
  6.  
  7. class MyClass : public MainClass, public AbstractClass
  8. {
  9.     Q_OBJECT
  10.  
  11. public:
  12.     Q_INVOKABLE MyClass();
  13.  
  14.     bool isAbstractClass() const;
  15. };
  16.  
  17. Q_DECLARE_METATYPE(MyClass*)
  18.  
  19. #endif // MYCLASS_H

main.cpp:

  1. #include <QtCore/QCoreApplication>
  2. #include "MyClass.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     QCoreApplication a(argc, argv);
  10.  
  11.     QMetaObject metaObj = MyClass::staticMetaObject;
  12.     QObject* obj = metaObj.newInstance();
  13.     AbstractClass* ac = (AbstractClass*) obj;
  14.     cout<<ac->isAbstractClass();       // insert breakpoint here. step into function
  15.  
  16.     return a.exec();
  17. }

MainClass.cpp:

  1. #include "MainClass.h"
  2.  
  3. MainClass::MainClass()
  4. {
  5. }
  6.  
  7.  
  8. MainClass::~MainClass()
  9. {
  10. }

AbstractClass.cpp:

  1. #include "AbstractClass.h"
  2.  
  3. AbstractClass::AbstractClass()
  4. {
  5. }

MyClass.cpp:

  1. #include "MyClass.h"
  2.  
  3. MyClass::MyClass()
  4. {
  5. }
  6.  
  7. bool MyClass::isAbstractClass() const {
  8.     return false;
  9. }

11 replies

August 5, 2011

loladiro loladiro
Lab Rat
596 posts

Is this what you need

  1. class MainClass : public QObject, public virtual AbstractClass
  2. class MyClass : public MainClass, public virtual AbstractClass
?

August 5, 2011

genC genC
Lab Rat
14 posts

No, MainClass can only inherit QObject.

August 5, 2011

loladiro loladiro
Lab Rat
596 posts

Ok, sorry I misunderstood. Your problem is that you’re trying to cast you QObject pointer to an Abstract class pointer using C-style casts. That’s not only unsafe, but doesn’t work in this situation. Since QObject, does not inherit from AbstractClass, you’ll have to use dynamic_cast .

August 5, 2011

genC genC
Lab Rat
14 posts

Well actually, the QObject pointer points to a MyClass instance. Since MyClass inherits AbstractClass, I should be able to cast the QObject pointer to a AbstractClass. Am I right?
But, correct me if I’m wrong, dynamic_cast only succeeds between classes of the same hierarchy (from base class to derived class), so it will fail in my case because AbstractClass doesn’t inherit QObject (and I don’t want to do it).

August 5, 2011

loladiro loladiro
Lab Rat
596 posts

Actually int points to the appropriate offset of the QObject class and the compiler doesn’t know that it’s a MyClass. Since you don’t want to use dynamic_cast, you can do:

  1. AbstractClass* ac = qobject_cast<MyClass*>(obj);

August 5, 2011

Volker Volker
Robot Herder
5428 posts

dynamic_cast
is a means of C++ and works for all sorts of class hierarchies (unless RTTI is not switched off)

qobject_cast
is provided by Qt and only works on QObject based class hierarchies

So you can safeley use dynamic_cast for your purpose. But be sure to check the resulting pointer not being a null pointer (in case something went wrong)

August 5, 2011

genC genC
Lab Rat
14 posts

dynamic_cast fails in my case (because QObject and AbstractClass aren’t related, I believe). that’s why I used c-style cast. But again it seems not to work (because of the whole cast inner working).
qobject_cast could’ve been interesting, but I really need to cast to AbstractClass* rather than MyClass*. And AbstractClass cannot inherit QObject…
I found a workaround, but it’s very bad design. I created a Interface class that all my abstract classes would inherit (including AbstractClass), and wrote a function that returns an Interface* given QObject*:

  1. Interface* getInterface(QObject* obj) {
  2.     if (obj == NULL) {
  3.         return (Interface*) NULL;
  4.     }
  5.     std::string name(obj->metaObject()->className());
  6.     if (name == "MyClass") {
  7.         return dynamic_cast<MyClass*> (obj);
  8.     }
  9.     else {
  10.         return (Interface*) NULL;
  11.     }
  12. }

This way, the cast succeeds. But since it is completely static, I’d rather find another way.

Any suggestions?

August 5, 2011

Gerolf Gerolf
Area 51 Engineer
3210 posts

If the QObject subclasses can be of any type and perhaps also derived not directly from QObject, there is no way for that.

If dynamic_cast fails, I assume, you have RTTI disabled, at least for Qt it’s disabled.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 7, 2011

Volker Volker
Robot Herder
5428 posts

dynamic_cast always works as long as you are navigating within a inheritance hierarchy. It fails only for two reasons:

  • the object you cast is not of the type you want to cast to
  • RTTI is switched off

As Gerolf already stated, it’s most likely the missing RTTI that causes you trouble.

If you use QMetaObject to get some class names, you can also use qobject_cast, as the first is available with QObjects only.

August 8, 2011

genC genC
Lab Rat
14 posts

Sorry for the late reply. I used dynamic_cast in other cases, and it worked. Only in this particular one it doesn’t.
In case RTTI is disabled, is there a way to enable it?

August 15, 2011

Volker Volker
Robot Herder
5428 posts

For Qt itself that’s configured at compile time with the -rtti switch.

For your projects, you can add rtti do CONFIG:

  1. CONFIG += rtti

 
  ‹‹ [SOLVED]Help on QDialog..      Main application window and a dialog interaction ››

You must log in to post a reply. Not a member yet? Register here!