[Interest] Clarification re destructor in subclass of QWidget

Marc Mutz marc.mutz at kdab.com
Thu Aug 16 19:27:18 CEST 2012


On Thursday August 16 2012, Shriramana Sharma wrote:
> My question: I am assuming that I have to include the library cleanup
> call in the destructor of MainWindow,

Alternatively, use a RAII (http://en.wikipedia.org/wiki/RAII) object in 
main() :

class LibraryInitAndDeinit {
    LibraryInitAndDeinit() { init_drawing_library(); }
   ~LibraryInitAndDeinit() { deinit_drawing_library(); }
};

int main( ... ) {
    const LibraryInitAndDeinit libraryIniter;
    QApplication ...;
}

> but if I write a custom 
> destructor, do I have to do any other delete calls to remove objects
> that are created on the heap in the constructor using new QTabWidget
> etc?

No. You cannot not call the destructor of the base class, no matter what you 
do (except throwing an exception). The destructor isn't like a normal virtual 
function in that respect.

> If I don't have to call the library cleanup manually, could I 
> simply leave out the destructor and the compiler will create the
> appropriate instructions itself?

Yes.

If you do add the destructor, make sure it's called (create the window on the 
stack or else if you create it on the heap, set the Qt::WA_DeleteOnClose flag 
on it).

HTH,
Marc

-- 
Marc Mutz <marc.mutz at kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
www.kdab.com || Germany +49-30-521325470 || Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-Independent Software Solutions



More information about the Interest mailing list