title: Pros & Cons of putting all code in Header files in C++?
body: <p>You can structure a C++ program so that (almost) all the code resides in Header files. It essentially looks like a C# or Java program. However, you do need at least one <code>.cpp</code> file to pull in all the header files when compiling. Now I know some people would absolutely detest this idea. But I haven't found any convincing downsides of doing this. I can list some advantages:</p>
  
  <p>[1] Faster compile times. All header files only get parsed once, because there is only one .cpp file. Also, one header file cannot be included more than once, otherwise you will get a build break. There are other ways of achieving faster compiles when using the alternate approach, but this is so simple.</p>
  
  <p>[2] It avoids circular dependencies, by making them absolutely clear. If <code>ClassA</code> in <code>ClassA.h</code> has a circular dependency on <code>ClassB</code> in <code>ClassB.h</code>, I have to put a forward reference &amp; it sticks out. (Note that this is unlike C# &amp; Java where the compiler automatically resolves circular dependencies. This encourages bad coding practices IMO). Again, you can avoid circular dependencies if your code was in <code>.cpp</code> files, but in a real-world project, <code>.cpp</code> files tend to include random headers until you can't figure out who depends on whom.</p>
  
  <p>Your thoughts?</p>