Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I repeat inclusions in .cpp and .h?

Scenario:

foo.h:

#include <vector>

class foo {
  public:
  std::vector<int>* getVector();

  /* ... other methods declarations ... */

}

foo.cpp:

#include "foo.h"
#include <vector>

/* ... other methods definitions using std::vector ... */

std::vector<int>* foo::getVector() {
  return new std::vector<int>();
}

I want .cpp to be independent of any possible future changes in the header. If for whatever reason the interface of the class changes and the dependency from <vector> can be eliminated, I risk that other methods in the .cpp also lose that inclusion.

Is it correct to repeat the inclusion of <vector> in both the .cpp and .h? Does this practice make sense or should I just rely on the inclusions made in the header?

like image 419
Andrea Casaccia Avatar asked Jul 24 '12 21:07

Andrea Casaccia


People also ask

Should includes be in header or CPP?

To minimize the potential for errors, C++ has adopted the convention of using header files to contain declarations. You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration.

Does every .h file need a .cpp file?

Cpp files don't always have to have a header file associated with it but it usually does as the header file acts like a bridge between cpp files so each cpp file can use code from another cpp file. One thing that should be strongly enforced is the no use of code within a header file!

Which of the following should generally not be included in header files?

Header files should never contain object definitions, only type definitions and object declarations.

Why should header files avoid having using declarations?

This can provoke ambiguities in client code, or even select the wrong function during overload resolution. A namespace 'using' declaration is not as polluting, but can have strange side effects since it only captures function overload sets at the time it appears.


3 Answers

Include what you need, and nothing more.

Including the same header file across multiple .h files and multiple .cpp files is not a problem in itself. Header guards are effective at mitigating problems from including files multiple times.

If you start trying to avoid including the same file multiple times, it can actually be negative as it usually leads to a "mega-include file" which includes everything you need in the entire project. This is bad, because one change to any header file causes everything to re-compile.

If you are worried about a .h/.cpp file both including the same file, then follow these guidelines:

  • If the include is not needed in the header file, only include it in the CPP
  • If a class declaration is needed in the header file (but not used), use forward declaration in the .h file and include it in the CPP file.
  • If you actually use the include within the header file, include it in the header file and not the CPP.
like image 193
riwalk Avatar answered Nov 14 '22 21:11

riwalk


In .cpp file, it is enough to include only things specific for implementation (what .cpp file in fact is), without repeating stuff you already included in header. This way, when somebody is looking at your code, he also gets a better and cleaner understanding of your code.

It can be very useful to know which dependencies are specific only to implementation, for example when you are upgrading/replacing it with another one (while preserving the interface).

like image 30
MatijaSh Avatar answered Nov 14 '22 21:11

MatijaSh


Include as few files in the header as possible, and only include them in the .cpp files that need them. Your foo.h header file might be included in many other .cpp files that do not need the declarations from the other header file(s) (in this case vector.h), which in the long run leads to longer compilations and less clear code.

like image 1
Avada Kedavra Avatar answered Nov 14 '22 20:11

Avada Kedavra