18

Looking at qwidget.h, I found the destructor as below:

~QWidget();

I was wondering why this is not declared as virtual

S B
  • 8,134
  • 10
  • 54
  • 108

1 Answers1

27

The destructor is virtual, because QWidget derives from QObject which does have a virtual destructor. Why it's not declared virtual in the code is a either style issue or a harmless mistake. I would have declared it virtual myself.

john
  • 85,011
  • 4
  • 57
  • 81
  • 5
    Actually, in C++ a method in a subclass is virtual if it is declared as such in the base class, so in this case the virtual keyword is not necessary for QWidget. Like John, I would also have added the virtual keyword for subclasses. – Tore Olsen Aug 18 '11 at 06:15
  • 2
    @john, I was under a false impression that `virtual` does not trickle down the inheritance chain implicitly. Turns out I was wrong. Thanks for pointing that out! – S B Aug 18 '11 at 06:22