views:

317

answers:

8

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 ?

+8  A: 

Nope. If there's nothing that needs to be in the .cpp, you don't need one.

Grumdrig
Unless you've have cyclic dependency somewhere in your design by design not by accident.
Chad
Not sure what you mean...please explain.
Grumdrig
If you have a cyclic dependency you wish to resolve in this way, then you do have something that needs to be in the .cpp, so Grumdrig's answer still holds.
jalf
+12  A: 

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.

Reed Copsey
I don't really agree, the code wrote inside a header has to be compiled as many times as it is included whereas it has to be compile only once if it is inside a source (.c) file. However I agree that it is quite convenient and compilation time is not a major issue to me.
Ben
@Ben, The assumption would be that any code that would be compiled in the h file would be inline sized code (or at least code that could be added to a precompiled header). Therefore, the compile time for the h file wouldn't be an issue.
kidnamedlox
@kidnamedlox: Exactly. @Ben: If you're including code that is of any size > than what should be inlined, I agree that a .cpp file is a good idea. It's only when the code is very small, I feel that putting an empty .cpp file just for the sake of a .cpp file is not good practice.
Reed Copsey
I would just add a comment on what you think should only be in a header file.
Martin York
+1  A: 

Rule of thumb, keep related class together in the same file. Majorly different Classes need to put into their own .hpp and .cpp files. The reason for this is in large projects you have up to 10,000 classes and to put that number of files infront of the user and IDE normally make things break.

Chad
+1  A: 

There are very useful and successful libraries, like many in boost, that are header-only.

Nikolai N Fetissov
+1  A: 

If some classes are short and seem to be inlineable, I tend to put them all together in types.h -- the only header which I don't consider to deserve a .cpp file.

However most classes outgrow both the chance to go into header only, and the chance to go into types.h.

Some examples from what I do. I consider class for implementation of three-dimensional vector Vector3 to deserve its .h and .cpp, despite being simple. But Position does not really deserve that; after all, it would even be a P.O.D. structure, if it weren't for that pesky getDistance() that I like implemented there.

So no -- not all classes deserve their .cpp and .h files. But most do, and those that don't definitely don't get to stand in a lone header. If they're so short to fit in header alone, either they cuddle together with other short classes, or go into the header of the class they're closely related to.

Ivan Vučica
+1  A: 

My rule of thumb is that any method I need more than one line of code to express goes into a .cpp file.

For a further discussion of this issue, you may want to check out this old question of mine: C++ code in header files. I don't think your question is an exact duplicate, but its close enough that you should read it.

T.E.D.
+1  A: 

Please also see this question - the header files should only be for declarations.

If you have compilable code included in multiple places (from your comment: "sould I avoid creating a .cpp file and writing the code directly on the header file ?"), it will be compiled each time (although you might have pre-compiled headers) and the linker will resolve (or not) the bloated object code.

Just because the class definition is small does not mean it should be in the header file unless the definition is also the same as the declaration - e.g. a pure virtual class or a simple type as mentioned in the other answer.

Cade Roux
Unfortunately, not all compilers support link-time inlining and even those that do have better inlining support at compilation time than at link-time. Therefore, you need to put small critical functions you want inlined into headers or do compile-time contortions like Unity Builds.
Adisak
A class definition **must** be in a header, if you intend to use the class in multiple Translation Units. Perhaps you were thinking of the definitions of the class _methods_ ? Those can safely be in the .cpp. The reason is because the linker can resolve functions and methods at link time, but not entire classes.
MSalters
@MSalters: You mean that a class **declaration** must be in a header.
quamrana
+3  A: 

Its really horses for courses:

  • Header only classes: eg template classes
  • Header and cpp: separates declaration from implementation.
  • cpp only: your main() can live in one of these.

Header file only source code is sometimes the only way of writing re-useable templates. See boost for plenty of examples of this.

Header and cpp is more 'normal'. Its separates the declaration from the implementation and can speed up compilation when the compiler doesn't have to read implementations loads of times. You might want to start here and then see how the implementation goes and if the cpp file become empty you can delete it.
Another point here is that you will have your #include "foo.h" at the top of foo.cpp to prove that anyone else can do this and not have compiler errors.

Cpp only files. main() can live here, and I've put cppUnit test classes in files like this.

quamrana