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. The keyword "new" and "this"
Forum Updated to NodeBB v4.3 + New Features

The keyword "new" and "this"

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 7.1k Views 2 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.
  • Z Offline
    Z Offline
    zxw2006063
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • Robey MardonR Offline
      Robey MardonR Offline
      Robey Mardon
      wrote on last edited by Robey Mardon
      #2

      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.

      Z 1 Reply Last reply
      0
      • T Offline
        T Offline
        t3685
        wrote on last edited by
        #3

        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.

        Robey MardonR 1 Reply Last reply
        0
        • T t3685

          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.

          Robey MardonR Offline
          Robey MardonR Offline
          Robey Mardon
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1
          • S Offline
            S Offline
            Sebastian
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • Robey MardonR Robey Mardon

              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.

              Z Offline
              Z Offline
              zxw2006063
              wrote on last edited by
              #6

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

              Xiaowei.

              Robey MardonR JKSHJ 2 Replies Last reply
              0
              • Z zxw2006063

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

                Xiaowei.

                Robey MardonR Offline
                Robey MardonR Offline
                Robey Mardon
                wrote on last edited by Robey Mardon
                #7

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

                1 Reply Last reply
                1
                • Z zxw2006063

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

                  Xiaowei.

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  Hi,

                  @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:

                  • QTimer constructor
                  • QObject constructor
                  • "Object Trees and Ownership"

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

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved