This appendix explains some of the mysteries of the C preprocessor, class declarations versus including headers, and some best practices to reduce dependencies between header files.
In C++, code reuse is indicated by the presence of a preprocessor directive, #include, in source code and header files. We #include header files that contain things like class or namespace definitions, const definitions, function prototypes, and so forth. These files are literally included in our own files before the compiler begins to translate our code.
The compiler will report an error if it sees any identifier defined more than once. It will tolerate repeated declarations but not repeated definitions.[1] To prevent repeated definitions, we are always careful to use an #ifndef wrapper around each header file. This tells the C preprocessor to skip the contents if it has already seen them. Lets examine the following class definition in Example C.1.
[1] We discuss the difference between declaration and definition in Section 20.1.
#ifndef CONSTRAINTMAP_H #define CONSTRAINTMAP_H /* included class definitions: */ #include
|
As you can see, as long as we use pointers or references, a forward declaration will suffice. The pointer dereferencing and member accessing operations are performed in the implementation file, which needs the full definition of all types it uses.
#include "constraintmap.h" ConstraintMap map; <-- 1 /* redundant but harmless if #ifndef wrapped */ #include "constraintmap.h" // Constraint p; <-- 2 #include
|
Here are some guidelines to help decide whether you need a forward declaration or the full header file to #include in your header file:
class ClassE;
A class that is declared but not defined is considered an incomplete type. Any attempt to dereference a pointer or define an object of an incomplete type will result in a compiler error.[2]
[2] The actual error message may not always be clear, and with QObjects, it might come from the MOC-generated code rather than your own code.
The implementation file, classa.cpp, for ClassA should #include "classa.h" and also #include the header file for each class that is used by ClassA (unless that header file has already been included in classa.h). All pointer dereferencing should be performed in the .cpp file. This helps reduce dependencies between classes and improves compilation speed.
A .cpp file should never #include another .cpp file. A header file should #include as few other header files as possible so that it can be included more quickly and with fewer dependencies. A header file should always be #ifndef wrapped to prevent it from being included more than once.
Part I: Introduction to C++ and Qt 4
C++ Introduction
Classes
Introduction to Qt
Lists
Functions
Inheritance and Polymorphism
Part II: Higher-Level Programming
Libraries
Introduction to Design Patterns
QObject
Generics and Containers
Qt GUI Widgets
Concurrency
Validation and Regular Expressions
Parsing XML
Meta Objects, Properties, and Reflective Programming
More Design Patterns
Models and Views
Qt SQL Classes
Part III: C++ Language Reference
Types and Expressions
Scope and Storage Class
Statements and Control Structures
Memory Access
Chapter Summary
Inheritance in Detail
Miscellaneous Topics
Part IV: Programming Assignments
MP3 Jukebox Assignments
Part V: Appendices
MP3 Jukebox Assignments
Bibliography
MP3 Jukebox Assignments