I have read the helpful article "When to use forward declaration?".
However, I am left with one a question. In the case were I can use a forward declare in the header, but not in the cpp is there a benefit and what is it?
I have read the helpful article "When to use forward declaration?".
However, I am left with one a question. In the case were I can use a forward declare in the header, but not in the cpp is there a benefit and what is it?
The benefit is reduced dependencies on your header files. Whenever, you do a #include
, the preprocessor basically adds that file into your header for compilation, this is called a dependency because your header needs it. So in large projects whenever the included file changes, the header file, its cpp file and everything which includes it also needs to be recompiled. Reducing dependencies generally can help reduce compile times in large projects.
Additionally, sometimes it is a requirement to break circular dependencies, which can often be found in sizable projects, e.g., file A includes file B, which includes file A again. So this is an added benefit of forward declaration.