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. Virtual destructors in Qt tutorial
Forum Updated to NodeBB v4.3 + New Features

Virtual destructors in Qt tutorial

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 3.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.
  • eric-rktE Offline
    eric-rktE Offline
    eric-rkt
    wrote on last edited by
    #1

    As a beginner in Qt programming, I am currently doing the Qt tutorial located at http://doc.qt.io/qt-5/gettingstartedqt.html that shows how to make a basic "Notepad" app.

    Under "Notepad Header File", just under halfway down the page, the tutorial describes the Notepad class destructor thus:

    "The following line declares a virtual destructor to free the resources that were acquired by the object during its life-cycle. According to the C++ naming convention, destructors have the same name as the class they are associated with, prefixed with a tilde (~). In QObject, destructors are virtual to ensure that the destructors of derived classes are invoked properly when an object is deleted through a pointer-to-base-class.".

    However, the destructor is simply "~Notepad();" with nary a "virtual" in sight. This was generated by Qt during the project creation.

    Was this an error on the part of the tutorial and Qt Creator author(s) or are destructors made virtual through some kind of Qt magic? Or, are we expected to add the "virtual" keyword ourselves?

    Thanks...

    Eric

    1 Reply Last reply
    1
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Hi, welcome to devnet.

      No, it's not an error and there's no magic, Qt or other, involved.
      In C++ any member that has a signature the same as a base virtual member is virtual with or without the keyword. In derived classes virtual is purely optional and used mainly for readability. In C++11 there's a new keyword override that has some extra features and is often used instead of (or in addition to) the extra virtual.

      The same goes for destructors too. If a class has a virtual destructor anything that inherits it has a virtual destructor, whether you make it explicit or not. As the tutorial says "In QObject, destructors are virtual (...)", so anything inheriting a QObject has a virtual destructor. Notepad class inherits QMainWindow which inherits QWidget, which inherits QObject, so its destructor is virtual implicitly.

      1 Reply Last reply
      1
      • eric-rktE Offline
        eric-rktE Offline
        eric-rkt
        wrote on last edited by
        #3

        Thanks, Chris, got it. I knew is was something simple. :-)

        Eric

        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