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. Introduction to QPointers
Qt 6.11 is out! See what's new in the release blog

Introduction to QPointers

Scheduled Pinned Locked Moved General and Desktop
11 Posts 6 Posters 5.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.
  • K Offline
    K Offline
    koahnig
    wrote on last edited by
    #2

    welcome to devnet

    As you stated some things are a matter of opinion.

    You can exclusively use Qt, but also mix with other libraries. It all is a matter of taste and how you have to get your job done. There is no "you must use" smart pointers of Qt, but they are there as well. That is the way I see it.
    I have started with smart pointers from boost a while, because they were to be included and part of standard libraries to C++. That was my criteria and I am continue to use them without problems with Qt.

    Vote the answer(s) that helped you to solve your issue(s)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #3

      Especially if you have lived without smart pointer classes so far, I wouldn't start making every pointer "smart". These classes (whether from Qt or another library) offer a benefit, but always at a cost. So before you use them, make sure to understand them.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #4

        Thanks for the replies. I think I get the basics, I'll see when/if I think they're appropriate.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #5

          This reminds me about this question:
          Qt and Borland C++ 64bit
          Is it possible to use Borland C++ with Qt. I would like to use Borland instead of GNU C++.
          http://qt-project.org/forums/viewthread/35303/
          Does anyone know an answer to it?
          I also wanted to know if I can use VSExpress 2010/2013 with Qt?

          Thanks!

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DerManu
            wrote on last edited by
            #6

            Just to make this clear: Are you talking about replacing pointers or smart pointers?

            Because in your original post, you say

            bq. this is the first one that actually “replaces” a basic C++ feature.

            So are you referring to the language feature "int *a" or are you referring to the standard library feature "std::shared_ptr<int> a" ?

            In the first case: No, you shouldn't eliminate the language feature of pointers, as smart pointers make only sense in certain situations. For example, turning a class member pointer which is exclusively internal into a smart pointer may be nonsensical, because you never actually have two pointers pointing at the same memory, of which at least one is out of the classes immediate control.

            In the second case: Yes, just as QString replaces std::string, you should use Qt's smart pointers instead of the STL's to have clean Qt code. Note that a should isn't a must, as koahnig has stated. Qt code will compile fine with STL or boost or any other implementation of smart pointers.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #7

              Thanks. I did mean the regular * type of pointer.
              Keeping a class member as a regular * does make sense as you say.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #8

                To clean up the vocabulary (and I've made this mistake myself): QPointer is not a "smart" pointer. QPointer can know whether the object behind it is still valid. However, real smart pointers do reference counting, and automatically delete an objects when it is no longer used. QPointer does not do that (but see QSharedPointer and QWeakPointer).

                This might, by the way, be a good indication on when to use QPointer:
                You have a pointer to an object, and this object can be deleted without you knowing it. This is pretty rare - usually you can notify your code in some way to reset your pointer to NULL. But if that is not an option for some reason, QPointer might be useful.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #9

                  [quote author="DerManu" date="1387042390"]For example, turning a class member pointer which is exclusively internal into a smart pointer may be nonsensical, because you never actually have two pointers pointing at the same memory, of which at least one is out of the classes immediate control.[/quote]

                  Well, keeping class members as a regular C++ may be a good idea, or it may be a good idea to use something like a QScopedPointer instead. It depends. [quote]There are many types of (smart) pointers in this world, and none of them should be used lightly![/quote]
                  to paraphrase Gandalf :)

                  It is not because some smart pointers are designed to deal with the case of sharing ownership of a resource, that all are. Every type has its own use. I beg to differ on the idea that QPointer is not a smart pointer. It is, in my opinion. It just does something slightly different than the smart pointers you're used to and it is specific to working on QObject (-derived) classes. But that does not make it less of a smart pointer.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by
                    #10

                    Maybe we can say that QSharedPointer is a "smart" pointer, whereas QPointer is a "clever" pointer?

                    ;-)

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #11

                      It is a matter of definition, but I think that the idea that a smart pointer always does some kind of resource owning is just flawed. Wikipedia "states":http://en.wikipedia.org/wiki/Smart_pointer:
                      [quote]In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic memory management or bounds checking. [/quote]
                      I think that's a workable definition. It is a class that acts as if it is a pointer, but provides some additional services. Those services may include reference counting, but in the case of QObject, it is resetting itself to 0 if the object pointed to is deleted.

                      Other types of smart pointers may perform other tricks, like checking the validity of calls on it.

                      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