Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C++ headers (.h) vs headers plus implementation (.h + .cpp), what are the disadvantages?

As a novice C++ programmer I have always put my classes interface in .h files and implementation in .cpp files. However I have recently tried C# for a while and I really like its clean syntax and way to organize files, in particular there is no dinstinction between headers and implementation, you usually implement a class for each .cs file and you don't need headers.

I know that in C++ this is also possible (you can code "inline" functions in .h files), but up to now I have always seen a clear distinction between .h and .cpp files in C++ projects. What are the advantages and disadvantages of this approach?

Thank you

like image 732
tunnuz Avatar asked Nov 10 '10 15:11

tunnuz


People also ask

Should I include 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.

Should I use H or HPP?

Fortunately, it is simple. You should use the . hpp extension if you're working with C++ and you should use . h for C or mixing C and C++.

Why .h is not used in C++?

h is deprecated and not a standard header. It was used in older programs before C++ was standardized, Functions like cout were defined inside iostream. h . After C++ got standardized, all these functions like cout got moved into the std namespace.To adjust to this change, non .

Do you always need a header file C++?

Overview. Every C++ program needs at least one header file to be meaningful. For example, most C++ programs need the cin object to take input from the user and much other pre-written code, which helps to make programming easier, so to use such functionalities, you need a header file.


3 Answers

There's a few ways that separating the two help in C++. Firstly, if you'd like to update a library without changing an interface then having the code in the C++ file means that you simply can update the library rather than the library plus the headers. Secondly it hides the implementation. That is, it forces people to look at your class only in terms of the interface, the thing that should concern them if the code is well written. Finally, there's a sort of asthetic cleanness with interface + documentation that comes with this separation. It's something you have to get used to but after a while it'll feel natural (opinion.)

like image 112
wheaties Avatar answered Oct 21 '22 03:10

wheaties


Don't forget build times.

Putting implementation code in header files makes them more likely to be changed. And changing header files will cause rebuilds of all the CPP files that include them, which in turn increases build times. This can be significant in larger projects.

I am also a fan of keeping the implementation hidden from users of my libraries. Unfortunately this doesn't work for template classes.

My rule of thumb: keep declarations in .H files, keep definitions in .CPP files.

like image 27
dripfeed Avatar answered Oct 21 '22 04:10

dripfeed


it's cooler to have the symbols defined at one place for the case you wanted to compound C++ with already compiled binaries (typicly when using a library). imagine you need to define external symbols for global stuff in your binaries. if you had .cpp and .h code in the same file you would have to define the symbols for your binaries for every such file. in two files way you could have just the one .h with definitions for binaries and a lot of .cpp files that use it.

like image 36
Pyjong Avatar answered Oct 21 '22 04:10

Pyjong