Class vs include



  • i'm reading the book "C++ Gui programming with Qt4" and i notice inside the code of the book,
    that sometimes inside the header files we use preprocessor command include:

    @#include <QDialogButtonBox>@

    and sometimes we use the declaration of class:

    @class QList;@

    Is there any rule when to use class and when to use class??

    Sorry for the question, and the language, and thanks in advanced.



  • In short, if the compiler doesn't need to know the size of an object or what methods it has, you can get away with not including the header - just do a forward declaration. For instance, if you declare a pointer to an object of a certain class, the compiler only needs to know that the class exists. If, however, you start calling methods on the object, the header has to be included.

    This was a short and simplified explanation, google for more info. Or brace yourself and read "this informative article":http://herbsutter.com/2013/08/19/gotw-7a-solution-minimizing-compile-time-dependencies-part-1/.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.