About #include directive
-
Hello there and sorry if my question look too simple or if I am at the wrong place to ask. I develop since a long time, but I am pretty new to c++ and even newer to Qt (a great tool by the way).
So here is the question: When I look about the Modules documentation, network module to be more accurate, at the end it says:
"To include the definitions of the module's classes, use the following directive: #include <QtNetwork>"
No problem so far and I understand the general idea behind the include. However, when I look a class documentation in the network module, let's say: QHostAddress, at the beginning it says:"The QHostAddress class provides an IP address. #include <QHostAddress>"Now I am confused, do I have to include both ? If I include only the Module, does it include automatically all the classes in this module? Do I include only the class? Does it have an impact on the compiled (executable) file, like size or speed?
Sorry again if it looks too simple for you, but like I said, I am pretty new to c++ and Qt.
Thanks in advance for your help.
Patrick
-
The rule is that each class has an include with the same name. So if you need class X you have to include file X. For convenience and to avoid a long list of includes in your code, you can include a few "special" includes that do the inclusions of each the classes for you. If you include both the includes are enough smart (i.e., they have macros) to prevent conflicts or double inclusion.
-
The "QtModule" includes will include all the headers in that module, so you don't need to include a particolar header again. The only impact they have is on compilation time: dozens of headers need to be opened, read, parsed, even if you're only using a small subset of them.
-
Wow fast answer. Thanks guys, things are more clearer now. Your help is really appreciated.
-
I'll kind of steal your topic, if I may, Patrick. Please?
OK, what I am, and always have been, curious about, are the "nested" includes like:
@
#include <QtCore/QString>
@Are they any different from simple:
@
#include <QString>
@
? -
At the moment: no.
But from Qt5, I believe the first version will become the standard. The idea is that we will have a much more modularized Qt, and you will see that reflected in the included. I now use the first form for new code. -
thanks a lot, that's one mystery less on my list :)
-
There's a little difference :-)
With the first form
@
#include <QtCore/QString>
@you only need one directory in the include path, e.g.
@
INCLUDEPATHS += /usr/local/Trolltech/Qt-4.7.4/include
@As you add the module's subdirectory in the #include directive, it will find every include file.
With the second form
@
#include <QString>
@you will explicitly have to add the QtCore submodule in your include path:
@
INCLUDEPATHS += /usr/local/Trolltech/Qt-4.7.4/include/QtCore
@While this is done automatically when using qmake, it might be of interest when setting up a build environment manually.
-
I see. Thanks again, this time to Volker :) Couldn't find a decent explanation for ages.
-
No problem to stealing my post, as long as it is profitable for all, and after reading your question, yes it is profitable. It is a good point and thanks to Andre and Volker for their answer. I will go with the first from from now on, to avoid a lot of corrections in the future when porting applications to Qt5
[quote author="sierdzio" date="1323324830"]I'll kind of steal your topic, if I may, Patrick. Please?OK, what I am, and always have been, curious about, are the "nested" includes like:
@
#include <QtCore/QString>
@Are they any different from simple:
@
#include <QString>
@
?[/quote]