Qt Meta-Object System and virtual functions with multiple inheritance - unexpected behaviour
-
No, MainClass can only inherit QObject.
-
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). -
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:
@
AbstractClass* ac = qobject_cast<MyClass*>(obj);
@ -
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?
-
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.
-
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.
-
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? -
For Qt itself that's configured at compile time with the -rtti switch.
For your projects, you can add rtti do CONFIG:
@
CONFIG += rtti
@