PDA

View Full Version : Class vs include



otttx33
7th February 2014, 22:18
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.

Infinity
8th February 2014, 03:13
You need to include the header file if you want to use the class (constructing instances, accessing members, ...).
If it is unnecessary to know the structure of the class (because you only need pointers to that class), use the forward declaration.

hammonjj
8th February 2014, 07:23
What you are seeing is a forward declaration. Like the poster above me mentioned, since the header only references the class, you only need to include the "class QList". However, you will notice that when the class is actually used (in the .cpp file for example, the #include <QList> must be included. People do this because it reduces compile times since you no longer need to include the header of a forward declared class in that files, only the file where the class (QList) is actually used. While not a big deal on pet projects, if you ever develop for a commercial entity, you're talking about serious time saved.

otttx33
11th February 2014, 19:29
thank youvery much