[Moved] Include files

  • This is really just a simple question.

    As I write my programs in Qt, I've noticed that I'm duplicating include files from class to class (i.e. QDebug, QString, QVector, etc.). Does this have a negative affect on the program? It doesn't seem to me that it would. Or, is this just a matter of style?

    I've considered just dumping all of the most common includes (Vector, Debug, etc.) in a single header file and then just reference that. For the more "specialized" includes, I would just leave them in the classes where they're used.

  • As far as I know, a common practice is that you do only forward declaration in the header file and do actual includes on source file. By doing this you ensure that you gain shorter compilation time, because header files are compiled more often and source files not.

  • I moved the thread to the C++ Gurus forum, since it's a general C++ topic, not specifically Qt related.

    If you only regard compiling as such, it is in practice irrelevant. While it is not allowed to define a class twice, this hardly ever happens. Every include file should have a so called "Include guard":http://en.wikipedia.org/wiki/Include_guard:

    #ifndef FOO_BAR_H
    #define FOO_BAR_H
    class FooBar {

    This way you can include the file as often as you want, but the actual contents are only included once (the first time you include the file). The naming conventions of the include guard vary.

    If you care about the speed your compiler works, it's better to avoid includes and replace them with forward declarations where possible (e.g. no use of a class in your own class - you would need to know the size of the class! Pointers and references are ok). This technique does NOT prevent from double includes!

    If you have a member of, say, type QString in your class:

    QString fooString;

    you must #include <QString> in your header file. Otherwise you may get a syntax error. It could of course be that the QString was already included by some other include (e.g. from a QWidget base class); then it might work. But you cannot rely on other includes to do the work for you if you do not control them, so it is good practice to always include the stuff you use yourself. If for what ever reason QString is taken out of the other include your compilation will break.

    You can, of course, put some common and often used includes into a separate file. This could save you some maintenance work, but it does neither affect compilation time nor correctness of your code.

  • For even faster (much) compilation, and some other nice side-effects (like binary compatibility for libraries) see the "d-ptr paradigm (Opaque pointer)":http://en.wikipedia.org/wiki/Opaque_pointer

    As far as the compilation speed goes - this way you can minimize the number of includes (a small speed boost) and you can change classes(1) without having chain reactions to recompile everything that uses them (big speed boost).


    (1) naturally, doesn't apply to all changes

Log in to reply

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