Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. India
  4. Inheriting from non-virtual dtor base class

Inheriting from non-virtual dtor base class

Scheduled Pinned Locked Moved India
4 Posts 2 Posters 2.0k Views
  • 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.
  • S Offline
    S Offline
    shanxS
    wrote on 16 May 2013, 00:44 last edited by
    #1

    I lately realized that classes like "QWidget ":http://qt.gitorious.org/qt/qt/blobs/4.8/src/gui/kernel/qwidget.h do not have a virtual dtor.

    But still lot many classes are using QWidget for public inheritance.

    And if I go by the guideline of "Herb Sutter - Guideline #4 in this article":http://www.gotw.ca/publications/mill18.htm then:

    bq. A base class destructor should be either public and virtual, or protected and nonvirtual.

    So what philosophy do we have in Qt regarding public base class's dtor ?
    And what about the semi-destruction of object when deleted from base class pointer ?

    Thanks for your time.
    shashank

    1 Reply Last reply
    0
    • P Offline
      P Offline
      podsvirov
      wrote on 16 May 2013, 05:15 last edited by
      #2

      You forgot class QObject.
      Just one virtual destructor at the top of the class hierarchy.

      Little example:
      @
      #include <iostream>

      using namespace std;

      class VirtualBase
      {
      public:
      VirtualBase()
      {
      cout << "VirtualBase: ktor (+1)" << endl;
      }
      virtual ~VirtualBase()
      {
      cout << "VirtualBase: dtor (-1)" << endl;
      }
      };

      class Base: public VirtualBase
      {
      public:
      Base()
      {
      cout << "Base: ktor (+1)" << endl;
      }
      ~Base()
      {
      cout << "Base: dtor (-1)" << endl;
      }
      };

      class Derive: public Base
      {
      public:
      Derive()
      {
      cout << "Derive: ktor (+1)" << endl;
      }
      ~Derive()
      {
      cout << "Derive: dtor (-1)" << endl;
      }
      };

      int main()
      {
      Base *base = new Derive();
      delete base;

      return 0;
      

      }
      @

      Output:
      @
      VirtualBase: ktor (+1)
      Base: ktor (+1)
      Derive: ktor (+1)
      Derive: dtor (-1)
      Base: dtor (-1)
      VirtualBase: dtor (-1)
      @

      Analogues:
      @
      virtual ~QObject(); // As VirtualBase
      ~QWidget(); // As Base
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shanxS
        wrote on 16 May 2013, 05:35 last edited by
        #3

        Got it! Thanks for example code. :)

        1 Reply Last reply
        0
        • P Offline
          P Offline
          podsvirov
          wrote on 16 May 2013, 07:06 last edited by
          #4

          Always happy to help on DevNet.

          1 Reply Last reply
          0

          1/4

          16 May 2013, 00:44

          • Login

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