If I have a project with
main .cpp
Knife .h and .cpp
Cucumber .h and .cpp
and I want to use Knife’s members in Cucumber, does it matter whether I use the following:
#include "Knife.h"
#include <iostream>
using namespace std;
in Cucumber.h or Cucumber.cpp (assume that Cucumber.cpp already has an include for Cucumber.h)?
My recommendation is to minimize the number of files included in the header files.
So, if I have the choice, I prefer to include in the source file.
When you modify a header file, all the files that include this header file must be recompiled.
So, if
cucumber.hincludesknife.h, andmain.cppincludescucumber.h, and you modifyknife.h, all the files will be recompiled (cucumber.cpp,knife.cppandmain.cpp).If
cucumber.cppincludesknife.handmain.cppincludescucumber.h, and you modifyknife.h, onlycucumber.cppandknife.cppwill be recompiled, so your compilation time is reduced.If you need to use knife in cucumber you can proceed like this:
This “trick” is called “forward declaration”. That is a well-known trick of C++ developers, who want to minimize compilation time.