[ 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.
You #include
header files that contain things like class
or namespace
definitions, const
definitions, function
prototypes, and so forth.
These files are literally included in your own files
before the compiler begins to translate your code.
The compiler reports an error if it sees any identifier defined more than once.
It will tolerate repeated declarations but not repeated definitions.[124] To prevent repeated definitions, be careful to use an
#ifndef
wrapper
around each header file. This tells the Preprocessor to skip the contents if it has already seen them. Let's
examine the following class definition in Example C.2
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.
Here are some guidelines to help decide whether you need a forward declaration, or the full header file #included
.
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.[125]
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. |