Should one use forward declarations instead of includes wherever possible?

The forward-declaration method is almost always better. (I can’t think of a situation where including a file where you can use a forward declaration is better, but I’m not gonna say it’s always better just in case).

There are no downsides to forward-declaring classes, but I can think of some downsides for including headers unnecessarily:

  • longer compilation time, since all translation units including C.h will also include A.h, although they might not need it.

  • possibly including other headers you don’t need indirectly

  • polluting the translation unit with symbols you don’t need

  • you might need to recompile source files that include that header if it changes (@PeterWood)

Leave a Comment