Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Introduction to QPointers



  • Hello,

    I've been using C++ for a while (although I wouldn't dare say I'm a "veteran" yet) but it was only recently that I decided to start learning Qt. After having been working on a "test project" for a bit (I find that I learn something a lot better by using it), I discovered the QPointer class and the similar ones (QWeakPointer etc).

    I've used other frameworks in the past but this is the first one that actually "replaces" a basic C++ feature, so I'm a little confused. Is someone working with Qt supposed/expected to use QPointers for all pointers to QObjects?

    I understand this is all a matter of opinion and "use what works best for you" but I was wondering what the general consensus is on this.

    To be honest, I'd have expected a tutorial about this kind of thing to be at the top of the list, or at least a web page like "So you've used C++ before, that's good, but here's a list of things you should do differently when using Qt".



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



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



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



  • 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!



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



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



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



  • [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.



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

    ;-)



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


Log in to reply