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:
@#ifndef MAINCLASS_H
#define MAINCLASS_H#include <QObject>
#include <QMetaObject>
#include <QVariant>class MainClass : public QObject
{
Q_OBJECTpublic:
MainClass();virtual ~MainClass();
};
Q_DECLARE_METATYPE(MainClass*)
#endif // MAINCLASS_H
@AbstractClass.h:
@#ifndef ABSTRACTCLASS_H
#define ABSTRACTCLASS_Hclass AbstractClass
{
public:
AbstractClass();virtual bool isAbstractClass() const = 0;
};
#endif // ABSTRACTCLASS_H
@MyClass.h:
@#ifndef MYCLASS_H
#define MYCLASS_H#include "MainClass.h"
#include "AbstractClass.h"class MyClass : public MainClass, public AbstractClass
{
Q_OBJECTpublic:
Q_INVOKABLE MyClass();bool isAbstractClass() const;
};
Q_DECLARE_METATYPE(MyClass*)
#endif // MYCLASS_H
@main.cpp:
@#include <QtCore/QCoreApplication>
#include "MyClass.h"
#include <iostream>using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QMetaObject metaObj = MyClass::staticMetaObject; QObject* obj = metaObj.newInstance(); AbstractClass* ac = (AbstractClass*) obj; cout<<ac->isAbstractClass(); // insert breakpoint here. step into function return a.exec();
}
@MainClass.cpp:
@#include "MainClass.h"MainClass::MainClass()
{
}MainClass::~MainClass()
{
}
@AbstractClass.cpp:
@#include "AbstractClass.h"AbstractClass::AbstractClass()
{
}
@MyClass.cpp:
@#include "MyClass.h"MyClass::MyClass()
{
}bool MyClass::isAbstractClass() const {
return false;
}
@ -
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 .
-
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). -
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 hierarchiesSo 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)
-
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*:@Interface* getInterface(QObject* obj) {
if (obj == NULL) {
return (Interface*) NULL;
}
std::string name(obj->metaObject()->className());
if (name == "MyClass") {
return dynamic_cast<MyClass*> (obj);
}
else {
return (Interface*) NULL;
}
}@This way, the cast succeeds. But since it is completely static, I'd rather find another way.
Any suggestions?
-
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.