Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Smart pointers in Qt

Smart pointers in Qt

Scheduled Pinned Locked Moved C++ Gurus
34 Posts 12 Posters 33.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.
  • G Offline
    G Offline
    gbonnema
    wrote on last edited by
    #20

    Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
    Specifically, does it render the Qt smart pointers obsolete?

    P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.

    My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.

    Anyone comments?

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #21

      Hi,

      [quote author="gbonnema" date="1418984633"]Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
      Specifically, does it render the Qt smart pointers obsolete?

      P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.

      My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.

      Anyone comments? [/quote]Qt smart pointers are not completely obsolete. Qt (and Boost) have more types of smart pointers than the C++11 standard. See the top answer at http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available

      Also note that QSharedPointer has the ability to perform "special casts":http://doc.qt.io/qt-5/qsharedpointer.html#objectCast on QObjects. std::shared_ptr doesn't have this ability.

      Having said that, it is perfectly fine to use C++11 shared pointers in a Qt project.

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

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #22

        Hi,

        [quote author="gbonnema" date="1418984633"]Now that smart pointers are an integral part of C++ 2011, does that change any of the considerations here?
        Specifically, does it render the Qt smart pointers obsolete?

        P.S. I just started C++ and Qt and I am very curious how to apply my freshly learnt C++ 2011 knowledge in combination with Qt.

        My gut feeling says that C++ smart pointers will eventually override the QT and Boost smart pointers, so for new programs the C++ pointers might be best.

        Anyone comments? [/quote]Qt smart pointers are not completely obsolete. Qt (and Boost) have more types of smart pointers than the C++11 standard. See the top answer at http://stackoverflow.com/questions/5026197/what-c-smart-pointer-implementations-are-available

        Also note that QSharedPointer has the ability to perform "special casts":http://doc.qt.io/qt-5/qsharedpointer.html#objectCast on QObjects. std::shared_ptr doesn't have this ability.

        Having said that, it is perfectly fine to use C++11 shared pointers in a Qt project.

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sandy.martel
          wrote on last edited by
          #23

          bq. std::shared_ptr doesn’t have this ability.

          yes it does, see std::dynamic_pointer_cast<>().

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sandy.martel
            wrote on last edited by
            #24

            bq. std::shared_ptr doesn’t have this ability.

            yes it does, see std::dynamic_pointer_cast<>().

            1 Reply Last reply
            0
            • JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #25

              [quote author="sandy.martel" date="1420514147"]bq. std::shared_ptr doesn’t have this ability.

              yes it does, see std::dynamic_pointer_cast<>().
              [/quote]std::dynamic_pointer_cast() is analogous to QSharedPointer::​dynamicCast(),. It relies on RTTI.

              I was referring to QSharedPointer::​objectCast(), which has no analogue in standard C++. It relies on Qt's "meta-object system":http://doc.qt.io/qt-5/metaobjects.html (no RTTI support required).

              QSharedPointer::​objectCast() works reliably across DLL boundaries, but QSharedPointer::​dynamicCast() and std::dynamic_pointer_cast() don't.

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

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #26

                [quote author="sandy.martel" date="1420514147"]bq. std::shared_ptr doesn’t have this ability.

                yes it does, see std::dynamic_pointer_cast<>().
                [/quote]std::dynamic_pointer_cast() is analogous to QSharedPointer::​dynamicCast(),. It relies on RTTI.

                I was referring to QSharedPointer::​objectCast(), which has no analogue in standard C++. It relies on Qt's "meta-object system":http://doc.qt.io/qt-5/metaobjects.html (no RTTI support required).

                QSharedPointer::​objectCast() works reliably across DLL boundaries, but QSharedPointer::​dynamicCast() and std::dynamic_pointer_cast() don't.

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

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sandy.martel
                  wrote on last edited by
                  #27

                  bq. I was referring to QSharedPointer::​objectCast(), which has no analogue in standard C++. It relies on Qt’s meta-object system [doc.qt.io] (no RTTI support required).

                  dynamic_cast is the analogue to qobject_cast, and RunTime Type Information is the (lot less feature rich) analogue to the meta-object system. Well, it's the other way around actually.

                  bq. QSharedPointer::​objectCast() works reliably across DLL boundaries, but QSharedPointer::​dynamicCast() and std::dynamic_pointer_cast() don’t.

                  I remember having problem with this, but that was 15 years ago, I think it's mostly resolved now. On the other hand, it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed. The dynamic_cast problem was the same in earlier gcc, duplicate typeinfo data for the same class, but it seems to work reliably now.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    sandy.martel
                    wrote on last edited by
                    #28

                    bq. I was referring to QSharedPointer::​objectCast(), which has no analogue in standard C++. It relies on Qt’s meta-object system [doc.qt.io] (no RTTI support required).

                    dynamic_cast is the analogue to qobject_cast, and RunTime Type Information is the (lot less feature rich) analogue to the meta-object system. Well, it's the other way around actually.

                    bq. QSharedPointer::​objectCast() works reliably across DLL boundaries, but QSharedPointer::​dynamicCast() and std::dynamic_pointer_cast() don’t.

                    I remember having problem with this, but that was 15 years ago, I think it's mostly resolved now. On the other hand, it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed. The dynamic_cast problem was the same in earlier gcc, duplicate typeinfo data for the same class, but it seems to work reliably now.

                    1 Reply Last reply
                    0
                    • JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #29

                      [quote author="sandy.martel" date="1420588259"]it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed.[/quote]Ah, I hadn't seen this before. Thanks for enlightening me.

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

                      1 Reply Last reply
                      0
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by
                        #30

                        [quote author="sandy.martel" date="1420588259"]it's pretty easy for a naive user to get qobject_cast to fail across dll boundaries. Just add the HEADERS of your shared class to both projects, the dll and the main. It's just a header shouldn't cause any harm? MOC will run twice, you'll end up with duplicate meta objects and qobject_cast will silently failed.[/quote]Ah, I hadn't seen this before. Thanks for enlightening me.

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

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #31

                          @_sandy.martel_ : Aren't you describing a wrong usage of HEADERS ? INCLUDEPATH should be used to use a header from a library project and not HEADERS

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #32

                            @_sandy.martel_ : Aren't you describing a wrong usage of HEADERS ? INCLUDEPATH should be used to use a header from a library project and not HEADERS

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              sandy.martel
                              wrote on last edited by
                              #33

                              Well, obviously this wrong, since it doesn't work correctly.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                sandy.martel
                                wrote on last edited by
                                #34

                                Well, obviously this wrong, since it doesn't work correctly.

                                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