Why do we need preliminary class declarations in the header file?

#ifndef UIBLUE #define UIBLUE #include QDialog class QCheckBox; class QLable; class QLineEdit; class UiBlue : public QDialog { Q_OBJECT }; #endif // UIBLUE 
  • one
    In the code that you gave, they are not needed in the least. Also: what is #include QDialog ? - AnT

1 answer 1

If your uiblue.h header file (I assume it is so called), uses the QCheckBox class (etc.) only to declare pointers / references to this class and does not attempt to access members of this class, then you You can try to save on compile time. Namely, you can <QCheckBox> header file in your header file, but simply perform preliminary declarations of the QCheckBox class and other required classes, as in your example.

You will have to include <QCheckBox> somewhere later, but with the appropriate organization of the definition of your UiBlue class UiBlue , you only need to include the <QCheckBox> in the implementation file (s) of the UiBlue class that work with QCheckBox class QCheckBox . Let's say it is a uiblue.cpp file.

Thus, other files of your project, which include uiblue.h , will not cause the indirect inclusion of the header files <QCheckBox> , <QLineEdit> , etc., which could potentially speed up the compilation of these other files.