Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is QWidget's destructor not virtual?

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

~QWidget();

I was wondering why this is not declared as virtual

like image 931
S B Avatar asked Aug 18 '11 05:08

S B


People also ask

Does destructor can be virtual?

Yes, it is possible to have a pure virtual destructor. Pure virtual destructors are legal in standard C++ and one of the most important things to remember is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor.

Why is destructor declared virtual?

A virtual destructor is used to free up the memory space allocated by the derived class object or instance while deleting instances of the derived class using a base class pointer object.

Is implicit destructor virtual?

The implicitly-declared destructor is virtual (because the base class has a virtual destructor) and the lookup for the deallocation function (operator delete()) results in a call to ambiguous, deleted, or inaccessible function.

Does destructor need to be virtual?

Virtual keyword for destructor is necessary when you want different destructors should follow proper order while objects is being deleted through base class pointer.


1 Answers

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.

like image 74
john Avatar answered Oct 20 '22 03:10

john