Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Meta-Object System and virtual functions with multiple inheritance - unexpected behaviour
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 7.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    loladiro
    wrote on last edited by
    #2

    Is this what you need
    @
    class MainClass : public QObject, public virtual AbstractClass
    class MyClass : public MainClass, public virtual AbstractClass
    @ ?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      genC
      wrote on last edited by
      #3

      No, MainClass can only inherit QObject.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        loladiro
        wrote on last edited by
        #4

        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 .

        1 Reply Last reply
        0
        • G Offline
          G Offline
          genC
          wrote on last edited by
          #5

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

          1 Reply Last reply
          0
          • L Offline
            L Offline
            loladiro
            wrote on last edited by
            #6

            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);
            @

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #7

              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)

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • G Offline
                G Offline
                genC
                wrote on last edited by
                #8

                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?

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #9

                  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.

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

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #10

                    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.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      genC
                      wrote on last edited by
                      #11

                      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?

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        goetz
                        wrote on last edited by
                        #12

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

                        For your projects, you can add rtti do CONFIG:

                        @
                        CONFIG += rtti
                        @

                        http://www.catb.org/~esr/faqs/smart-questions.html

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved