Why use .cpp files if I have all the C ++ code in the .h file?

Why use .cpp files if I can use all my C ++ code in a .h file? I mean .cpp files are pretty weird to use if all the code can be written in a .h file? Can anyone be clerefy?

+6
c ++ oop header
source share
3 answers

A few reasons:

(1) Incremental build times

When projects grow, managing build time is problematic, especially for C ++ projects. Building 1 or 5 minutes after minor changes is of great importance. This is underlined by most of the changes in large projects, which are small and require a lot of testing. Add to that any TDD attempt and refactoring, and you are dead slime with Sicilian shoes.

Separating the header and body and moving it to libs significantly increases the incremental build time.

(2) Statics
For many things, you need one type instance, i.e.

// .cpp static Foo foo; 

There is no way (what I know) that allows this in the project for header only. Specific solutions for the compiler are limited, for example. __declspec(selectany) in MSVC is limited to POD types.

(3) Hiding implementation
.cpp / .h is the only way to clearly separate the public interface from implementation details. You can move class members to the private section, but this does not work for other objects. (Even the header / body separation is leaky if you do not add additional methods such as PIMPL, so this argument is a bit weak IMO, but again, in a large project, I would very much miss this effective if imperfect method).


Great question, anyway, you admitted that there is something unpleasant with the C / C ++ assembly model, which I consider an ancient relic of terrible consequences.

You should try how far you can push the heading-only model (or at least β€œheading-only” to allow static). You could get pretty far - it would also be interesting to hear from people who have tried.

Perhaps you should try using static libraries to separate and encapsulate implementations, and otherwise save all your code in headers. I see some problems with this, but it is not that our current modus operandi is not possible.

+7
source share

You can put all your code in .h files. Contrary to popular belief, this will not duplicate code through your .obj files. Modern compilers are much smarter than that.

Compilation is a bit of a problem. If you have 20 .h files that are included in main.cpp, compiling main.cpp will take some time. And it will be recompiled, including all 20 of your .h files. Every time one of your included files changes.

Then there is style. It just looks wrong. But this is a matter of preference.

Then there are links. If ClassA uses ClassB and ClassB uses ClassA, to which you first include?

+3
source share

Header files (.h) are for defining an interface so that your classes and code can be used in other translation units. If you put the implementation in the .h file, then you will get several copies of the same code compiled into each translation unit that includes this .h file. This hits the point of dividing your code into small pieces that you can study and develop in isolation.

+2
source share

All Articles