[Qt-interest] include modules vs. files
andrew.m.goth at l-3com.com
andrew.m.goth at l-3com.com
Tue Feb 3 01:31:48 CET 2009
Scott Aron Bloom wrote:
> However, compile time will change, since your including a lot more...
> And your dependencies wont be minimized..
In many cases you can further improve compile times and reduce
dependencies* by not #including the header file at all. Instead just
forward-declare the classes. Example:
#include <QWidget>
class QPushButton;
class MyWidget: public QWidget {
Q_OBJECT
public:
MyWidget(QWidget* = NULL);
protected:
QPushButton* button;
};
QWidget has to be #included since a class is being derived from it. But
QPushButton does not have to be #included since the only thing it is
being used for is declaring a pointer to it. The reason is that the
compiler doesn't need to know anything about it, except that it is a
class, in order to know how much space to allocate for MyWidget::button,
since the size of a pointer to a class instance doesn't depend on the
definition of the class.
(*) The main reason to minimize dependencies is to reduce the number of
files that have to be recompiled when a header file is changed. I know
you're probably not going to be changing the QPushButton header file,
but get in the habit anyway since this technique is just applicable for
header files within your project. There are few worse things than
fixing a typo in a comment in a header file, then having to wait ten
minutes for your entire project to rebuild as a result!
--
Andy Goth
<amgoth at link.com>
More information about the Qt-interest-old
mailing list