:: printable version ::

Pre-declare classes when possible

You could speed up compilation by replacing #includes with class pre-declarations in your header files. If your class declaration only defines pointers to a given class, you can #include the header of that class in your .cpp file. Here is an example:

// *** myclass.h ***
#include <QMap>
// we create a QMap instance so we need an #include
class QPushButton;
// we only define pointers to QPushButton objects
class QListView;
// we can avoid an #include here too
// ...
class myClass { public: myClass(QPushButton* someButton); void init() { myMapple = new QMap; } private: QPushButton* myButton; QMap* myMapple; };

The class implementation would look like this:

// *** myclass.cpp ***
#include "myclass.h" #include <QPushButton>
// here come the #include
#include <QTreeWidget>
// for the pre-declared classes
// ...
myClass::myClass(QPushButton* someButton) {
// ...
myButton = new QPushButton;
// ...

The trick works because make depend won't try to recompile every .cpp file including our myclass.h each time qpushbutton.h or qlistview.h (in other words, the headers for the classes we pre-include) change.

This little trick was suggested by Trolltech's official Qt reference documentation.

:: index :: top ::