tags:

views:

68

answers:

2

hello everyone, I've just begun to study qt using qt-creator, I found some tutorials int which the author doesn't use d'tors at all? is it good practice? or it will be better to manage my objects destruction, thanks in advance

+2  A: 

When a class doesn't declare a destructor, the compiler automatically gives it a definition equivalent to {}. Often in Qt, this is good enough, even if the class contains pointers. Whenever something derived from QObject is destroyed, Qt automatically deletes all its children (recursively) as well, as long as each child was passed a "parent" pointer.

If there's anything you think your class's destructor should do, definitely define it yourself.

One other quirk: If the destructor is the only virtual method defined in the class, providing your own non-inline definition can make generation of C++ magic data (vtables) easier on the linker on some systems.

After all the above is considered, whether allowing defaulted destructors is good practice is a matter of opinion. (When I do allow a default destructor, copy constructor, default constructor, and/or assignment operator, I prefer to say that in comments in the public section of the class definition.)

aschepler
Official documentation: http://doc.qt.nokia.com/4.6/objecttrees.html
Giuseppe Cardone