Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use .cpp files if I can have all of my C++ code in .h file?

Tags:

c++

oop

header

Why to use .cpp files if I can have all of my C++ code in .h file? I mean .cpp files are quite strange to use if all code can be wrote in .h file? Can any one clerefy?

like image 309
Rella Avatar asked Dec 25 '10 17:12

Rella


2 Answers

You could put all your code into .h files. Contrary to popular belief, this won't duplicate the code across your .obj files. Modern compilers are much smarter than that.

Compilation is somewhat an issue though. If you have 20 .h files all included into main.cpp, compiling main.cpp will take a while. And it will be recompiled, including all 20 of your implementation .h files, every time one of your include files changes.

Then there's style. It just looks wrong to me. But this is a matter of preference.

Then there are references. If ClassA uses ClassB, and ClassB uses ClassA, which one do you include first?

like image 21
martona Avatar answered Nov 05 '22 18:11

martona


A few reasons:

(1) Incremental Build Times

When projects grow larger, managing the build time is problematic especially for C++ projects. Building 1 or 5 minutes after a minor change makes a big difference. This is emphasized by most changes in large projects being small and require a lot of testing. Add to that any attempt of TDD and refactoring, and you are a dead slug with sicilian shoes.

Splitting into header and body, and moving it to libs improves incremental build times tremendously.

(2) Statics
For many things you need a single instance of a type, i.e.

// .cpp
static Foo foo;

There is no way (that I'm aware of) allowing this in a header-only project. Compiler specific solutions are limited, e.g. __declspec(selectany) in MSVC is limited to POD types.

[edit] C++17 now allows inline also for variable initialization, so this is not a blocking issue anymore.

(3) Implementation hiding
.cpp / .h separation is the only way to clearly separate a public interface from implementation details. You can throw class members into a private section, but that doesn't work for other entities. (Even the header/body separation is leaky unless you add additional techniques such as PIMPL, so this argument is a bit weak IMO, but again, in a large project I'd dearly miss this efficient if imperfect method).


Great question, anyway - you've recognized that there's something afoul with the C/C++ build model, which I consider an ancient relic of horrible implications.

You should try how far you can push a "header only" model (or, at least, a "almost only headers", to allow for statics). You might get quite far - also it would be interesting to hear from people who have tried.

It might be worth a try to use static libraries to separate and encapsulate implementations, and otherwise keep all your code in headers. I can see a few problems with that, but it's nto that our current modus operandi is trouble free.

like image 130
peterchen Avatar answered Nov 05 '22 19:11

peterchen