Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Qt class in header file [duplicate]

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?

like image 774
zh_ Avatar asked Mar 11 '16 05:03

zh_


1 Answers

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.

like image 101
bibi Avatar answered Nov 16 '22 08:11

bibi