Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    qobject_cast() vs dynamic_cast()

    General and Desktop
    qobjectcast dynamiccast
    2
    4
    16363
    Loading More Posts
    • 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.
    • B
      Bart_Vandewoestyne last edited by Bart_Vandewoestyne

      Hello all,

      I am trying to understand why I should prefer qobject_cast over dynamic_cast in certain situations. Based on these three references:

      • http://doc.qt.io/qt-4.8/metaobjects.html
      • http://doc.qt.io/qt-4.8/qobject.html#qobject_cast
      • http://www.ics.com/designpatterns/book/qtrtti.html

      I come to the conclusion that the three advantages of using qobject_cast are:

      1. it doesn't require RTTI support
      2. it works across dynamic library boundaries
      3. it may be 5 to 10 times faster, depending on what compiler one uses

      I don't know how I could practically illustrate advantage 1. Furthermore, I do not understand advantage 2. Therefore, I would like to illustrate all three advantages with a practical test. I have started something that can be found at https://github.com/BartVandewoestyne/Qt/tree/master/tests/qobject_cast_test but now I'm rather stuck. I don't know how to proceed in order to practically illustrate the above three advantages. The speed gain is probably the easiest to illustrate... If anybody can help me out: suggestions, code-snippets or pull-requests are more than welcome!

      1 Reply Last reply Reply Quote 0
      • B
        Bart_Vandewoestyne last edited by

        In the meanwhile, I found out that each compiler has its own way of enabling or disabling RTTI. I've made a small test-program to illustrate how dynamic_cast is no longer available when RTTI is disabled, see https://github.com/BartVandewoestyne/Cpp/blob/master/examples/C%2B%2B98/rtti.cpp

        I've also added an option to enable or disable RTTI to the .pro file at https://github.com/BartVandewoestyne/Qt/tree/master/tests/qobject_cast_test so users can see for themselves that qobject_cast remains available when RTTI is disabled, but dynamic_cast is no longer available.

        So I now know how to illustrate advantage 1. Still remains me to illustrate advantage 2 and 3... Any help appreciated.

        1 Reply Last reply Reply Quote 0
        • JKSH
          JKSH Moderators last edited by

          Hi @Bart_Vandewoestyne,

          For #2, see http://stackoverflow.com/questions/1964746/dynamic-cast-returns-null-but-it-shouldnt

          For #3, you could instantiate 100000 QObject-derived objects, and cast them in a loop. Measure the time it takes to run the loop.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply Reply Quote 0
          • B
            Bart_Vandewoestyne last edited by

            In the meanwhile, I have code for #3 too, see https://github.com/BartVandewoestyne/Qt/tree/master/tests/qobject_cast_test I'm sure it can be improved and made more elegant (see e.g. the TODO in the file). Comments, suggestions or pull requests are definitely still welcome!

            When I run this program on my computer, it shows me that qobject_cast is about 6 to 13 times faster than dynamic_cast.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post