[ fromfile: preprocessor.xml id: preprocessor ]
In C++, code reuse is indicated by the presence of a preprocessor directive, #include
, at the top of source code files.
Example C.2. src/preprocessor/constraintmap.h
As you can see, inside function parameter lists, you can use pointers or references to classes which were only declared, not defined.
The pointer dereferencing and member accessing operations are performed in the implementation file shown in Example C.3.
There, you must #include
the full definitions of each type it uses.
Example C.3. src/preprocessor/constraintmap.cpp
To minimize the number of "strong dependencies" between header files, you should declare classes instead of #including
header files whenever posible.
If ClassA derives from ClassB, the definition of ClassB must be known by the compiler when it processes the definition of ClassA.
Therefore, the header file for ClassA must #include
the header file for ClassB.
If the definition of ClassA contains a member that is an object of ClassD, the header file for ClassA must #include
the header file for ClassD.
If the definition of ClassA contains a function that has a parameter or a return object of ClassD, the header file for ClassA must #include
the header file for ClassD.
If the definition of ClassA only contains nondereferenced ClassE pointers or references, then a forward declaration of ClassE is sufficient in the ClassA 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 results in a compiler error.[91]
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
).
Any 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.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |