Qt World Summit: Submit your Presentation

The keyword "new" and "this"

  • Hi,
    I found this code in the Analog Clock Example:
    QTimer *timer = new QTimer(this);

    I don't understand the keyword "new" and keyword "this" here. I mean what it means by using "this" in the brackets together with "new". I understand these two keywords separately. But don't understand them here.

    Many thanks.

  • The this and the new keyword is not Qt related, but C/c++ - or even other object-oriented language.

    Qt is great, although I recommend you to read about programming logic and the basics of C/C++ first.

    But in advance...

    The new keyword is to create an instance of a class, becoming an object.
    The this keyword is used within a class, that represents the object itself.

    Explaining everything for you steps: QTimer is a class which is being instantiated as timer, as you can see * means that timer has the pointer of that instance. The new as I described above means that you are instantiating a new class, and the (this); you are just passing the current object as parameter to the QTimer class.

  • The Qt library uses a parent system to do resource management. Normally when you use "new", you need to use delete at some point to free the memory on the heap again. A QTimer object (and QObject classes in general) take as an argument in their constructor a pointer to another QObject, which is then considered its parent. When the parent is destroyed, it also deletes its children. That's why you don't see "delete timer" in the destructor of "this". The parent-child system has other uses as well and you can look them up in the documentation.

  • @t3685 You have to be careful about that sometimes. If you pass a pointer as parameter and don't delete that pointer after you application is going to leak.

    The parent system isn't a solution for everything.

    ... just as complement to the comment above.

  • These two keywords comes from C++ concept.
    The** new** keyword allocates your string to the heap. while your first example allocated memory to the stack.
    Stack memory is not as readily available as heap memory. Basically, you store memory on the heap with the new and store that adress in a pointer. Memory on the heap can also be deleted. Using the 'delete' command. Which makes your program run more efficient.

    Every object in C++ has access to its own address through an important pointer called **this **pointer. The **this **pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.

  • @Robey-Mardon Thanks to your answer. But I still need to get one thing clear. We know that QTimer is a class and "this" refers to the address of the current object whose function is being executed. So why the class QTimer takes the address of current object as input? That's what I don't understand. Thanks again.


  • @zxw2006063 The QObject is an implementation of the composite design pattern and the pattern requires that the composite object takes ownership of the children. As long as the parenting has been done, you can be assured that the child QObjects will be destroyed when the parent is destroyed. That is the concept of parents and hierarchy. Without that you would need to delete the pointer after manually - pretty much.

  • Moderators


    @zxw2006063 said:

    So why the class QTimer takes the address of current object as input? That's what I don't understand.

    See the documentation for:

Log in to reply