Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to always create a .cpp for each .h in a C++ project?

Some classes, like exceptions or templates, only need the header file (.h), often there is no .cpp related to them.

I have seen some projects were (for some classes) there aren't any .cpp files associated to the headers files, perhaps because the implementation is so short that it is done directly in the .h, or maybe for other reasons, such as template classes, where it is mandatory to include the implementation in the header.

What is your opinion, if a class is too short, sould I avoid creating a .cpp file and writing the code directly on the header file ? If the code is written in the header file, should I include an empty .cpp so the files in the project remains consistent ?

like image 340
lurks Avatar asked Oct 29 '09 17:10

lurks


People also ask

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!

What is the difference between a .cpp file and a .h file?

The short answer is that a . h file contains shared declarations, a . cpp file contains definitions and local declarations. It's important that you understand the difference between declarations and definitions.

Should I include libraries in header or CPP?

Include it where its needed. If you use something defined in <iostream> in the declaration of the class (like a member variable, a member function parameter or return type, etc), then it should be in the H file. If you only use it in the implementation - then in the CPP.

Why we need header files at the top of each C++ program?

C language has numerous libraries that include predefined functions to make programming easier. In C language, header files contain the set of predefined standard library functions. You request to use a header file in your program by including it with the C preprocessing directive “#include”.


2 Answers

I wouldn't add unnecessary .cpp files. Each .cpp file you add must be compiled, which just slows down the build process.

In general, using your class will only require the header file anyways - I see no advantage to an "empty" .cpp file for consistency in the project.

like image 79
Reed Copsey Avatar answered Nov 15 '22 20:11

Reed Copsey


There is one advantage to always creates a .cpp for each .h, even when the former would be empty: it enforces at compile time that header files are self-contained. This is related with the guideline of including foo.h first in foo.cpp:

With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cpp will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages.

like image 34
Boris Dalstein Avatar answered Nov 15 '22 19:11

Boris Dalstein