+ 1

What are the advantages of separating the header file from the implementation file in C++?

It is common practice to separate the class code into the .h header file and the .cpp implementation file. Can you list the advantages or disadvantages of such an approach. Is it necessary and why?

14th Nov 2017, 10:31 PM
Red Hawks
Red Hawks - avatar
2 Answers
+ 3
The advantage is in larger project, which includes more source files. (maybe from more programmers). So when program calls some function (or create object form class) in one file from second, it must to know declaration of this function (class)- and it is in header file. In fact there is a main characteristic of C++. The every source file is compiled separately. And after they might be linked into one binary (runable) file. As I write, they might. It is possible to links only the main part of program, and the next parts (classes definitions, functions) are saved in external libraries.. So the header file is the necessary way how to manage it.
15th Nov 2017, 12:01 AM
Petr Hatina
Petr Hatina - avatar
+ 1
additional thought: if you use incremental compilation, you don't have to compile all code. but only the single cop you changed. through the header the dependence are resolved. or short: it helps to compile faster
15th Nov 2017, 7:08 AM
Gunther Strauss
Gunther Strauss - avatar