Using forward declarations instead of a full #include
s is possible only when you are not intending on using the type itself (in this file's scope) but a pointer or reference to it.
To use the type itself, the compiler must know its size - hence its full declaration must be seen - hence a full #include
is needed.
However, the size of a pointer or reference is known to the compiler, regardless of the size of the pointee, so a forward declaration is sufficient - it declares a type identifier name.
Interestingly, when using pointer or reference to class
or struct
types, the compiler can handle incomplete types saving you the need to forward declare the pointee types as well:
// header.h
// Look Ma! No forward declarations!
typedef class A* APtr; // class A is an incomplete type - no fwd. decl. anywhere
typedef class A& ARef;
typedef struct B* BPtr; // struct B is an incomplete type - no fwd. decl. anywhere
typedef struct B& BRef;
// Using the name without the class/struct specifier requires fwd. decl. the type itself.
class C; // fwd. decl. type
typedef C* CPtr; // no class/struct specifier
typedef C& CRef; // no class/struct specifier
struct D; // fwd. decl. type
typedef D* DPtr; // no class/struct specifier
typedef D& DRef; // no class/struct specifier