Forward Reference

One way of RefactoringCppToReduceDependencies is to use forward references. Suppose we have a class Beta that uses a class Alpha. Instead of writing Beta.h as

 #include "Alpha.h"
 class Beta
 {
  Alpha *a;
 };

we write

 class Alpha;
 class Beta
 {
  Alpha *a;
 };

and delay the inclusion of alpha.h to beta.cpp. That way things that depend on beta.h aren't forced to depend on alpha.h.

This is also a good way to avoid cyclic file dependencies.


EditText of this page (last edited April 11, 2003) or FindPage with title or text search