Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Qt base class virtual destructor

Do we need a virtual destructor for a classes which are gonna be used in Qt-way: set QObject-parent which will call in QObject's destructor deleteLater() or something like that for any object for which it was set as parent?

For example:

class MyWidget : public QWidget {
public:
    MyWidget() {
        w = new QWidget(this);
        // "w" will be deleted automatically by parent MyWidget::QWidget::QObject's destructor afaik
    }
private:
    QWidget *w;
}

Do we need a virtual destructor for MyWidget class if it is gonna be inherited? I see no reason for this because it does not delete anything and each property of the class which is derived from QObject will be deleted from MyWidget::QWidget::QObject's destructor.

like image 601
Victor Polevoy Avatar asked Feb 27 '15 13:02

Victor Polevoy


1 Answers

It doesn't matter if you add it or not as QObject inherited by QWidget has a virtual destructor, and this is propagated through the whole hierarchy.

like image 177
Bogdan Avatar answered Sep 21 '22 03:09

Bogdan