Thursday, February 27, 2014

C++ Avoid Circular Header File Dependencies

In C++, you will get compile errors when header files have a circular dependence on each other. Therefore, it is best to use forward declarations in the header files, and include the header files only in the "cpp" definition files.

However, there are 2 exceptions to this, where you will need to include another header file in the header file:

  1. When you are inheriting from another class, you will need the header class for that class in the header file
  2. When you are using a class as a stack variable rather than dynamically allocated heap variable, you will also need the header file for that class in the header file

No comments:

Post a Comment