I am reading book about using Qt5 (author Max Schlee), and I noticed that some examples have declared existing Qt class in the header file, for example like this:
class QProgressBar;
So, my question – why don't we just include the header file QProgressBar
without declaring class QProgressBar;
in our header file?
It's not about Qt, it's c++.
It's called forward declaration.
Basically in the .h
you just say that QProgressbar
is a class and do not complain about the fact that it's not defined. Then in the .cpp
file you put the header, so that at compile time, the compiler will have everything well defined.
This can save compile time, as #include
force the compiler to open more files and process more input.
This can also save on unnecessary recompilation. #include
can force your code to be recompiled more often, due to unrelated changes in the header.
Of course on large projects you can have drawbacks.
You can find more HERE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With