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

For some classes, such as exceptions or templates, only a header file (.h) is needed, often there is no .cpp associated with them.

I saw that some projects were (for some classes), there are no .cpp files associated with header files, perhaps because the implementation is so short that it is done directly in .h, or maybe for other reasons, such as classes templates where it is mandatory to include the implementation in the header.

What is your opinion, if the class is too short, can I create a .cpp file and write the code directly in the header file? If the code is written in the header file, should I include an empty .cpp so that the files in the project remain consistent?

+6
c ++ header-files compilation
source share
9 answers

I would not add unnecessary .cpp files. Each .cpp file you add must be compiled, which simply slows down the build process.

In general, using your class will only require a header file - I do not see the benefits of an "empty" .cpp file for consistency in the project.

+14
source share

Nope. If there is nothing that should be in .cpp, you do not need it.

+8
source share

Its really horses for courses:

  • Class headers only: for example, template classes
  • Header and cpp: separate the declaration from the implementation.
  • Only cpp: your main () can live in one of them.

The source code of the source header file is sometimes the only way to write reusable templates. See boost for a lot of examples of this.

Headers and cpp are more "normal". It separates the declaration from the implementation and can speed up compilation when the compiler does not need to read implementations multiple times. You can start here and then see how the implementation goes, and if the cpp file becomes empty, you can delete it. Another point is that you will have #include "foo.h" at the top of foo.cpp to prove that someone else can do this, not a compiler error.

Cpp files only. main () can live here, and I put the cppUnit test classes in such files.

+3
source share

Rule, save the associated class together in one file. Basically, different classes should put .hpp and .cpp in their files. The reason is that in large projects you have up to 10,000 classes for placing this number of files in front of the user, and the IDE usually breaks.

+1
source share

There are very useful and successful libraries, like many of boost , that only have a title.

+1
source share

If some classes are short and seem to be integral, I try to combine them into types.h - the only heading that I do not consider worthy of a .cpp file.

However, most classes outgrow both the chance to get into the title and the ability to enter types.h .

Some examples of what I'm doing. I am considering a class for implementing the Vector3 three-dimensional vector in order to earn it .h and .cpp, even though it is simple. But Position doesn't really deserve it; in the end, it would even be a POD structure, if not for this arrogant getDistance() , which I like to implement there.

So, no - not all classes deserve their .cpp and .h files. But most of them, and those that definitely cannot stand in a single headline. If they are so short that they fit only in the title, either they hug together with other short classes, or they are included in the title of the class with which they are closely related.

+1
source share

My rule of thumb is that any method that requires more than one line of code for an expression goes into a .cpp file.

For further discussion of this problem, you can check out this old my question: C ++ code in header files . I don't think your question is an exact duplicate, but it is close enough for you to read it.

+1
source share

Also see this question - header files should be for ads only.

If you have compiled code included in several places (from your comment: β€œCan't I create a .cpp file and write the code directly in the header file?”), It will be compiled every time (although you can have precompiled headers) , and the linker will allow (or not) bloated object code.

Just because the class definition is small does not mean that it should be in the header file if the definition does not match the definition - for example. pure virtual class or simple type as indicated in another answer.

+1
source share

There is one advantage to always creating .cpp for each .h, even if the first one is empty: it provides header files self-sufficient at compile time. This is related to the directive , including foo.h first in foo.cpp:

In the preferred order, if dir2 / foo2.h does not contain any necessary inclusions, the dir / foo.cpp build will be broken. Thus, this rule ensures that assembly breaks are displayed first for people working on these files, and not for innocent people in other packages.

+1
source share

All Articles