Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages and disadvantages of implementing classes in header files?

Tags:

c++

I love the concept of DRY (don't repeat yourself [oops]), yet C++'s concept of header files goes against this rule of programming. Is there any drawback to defining a class member entirely in the header? If it's right to do for templates, why not for normal classes? I have some ideas for drawbacks and benefits, but what are yours?

like image 713
May Oakes Avatar asked Nov 23 '09 15:11

May Oakes


2 Answers

Possible advantages of putting everything in header files:

  • Less redundancy (which leads to easier changes, easier refactoring, etc.)
  • May give compiler/linker better opportunities for optimization
  • Often easier to incorporate into an existing project

Possible disadvantages of putting everything in header files:

  • Longer compile/link cycles
  • Loss of separation of interface and implementation
  • Could lead to hard-to-resolve circular dependencies
  • Lots of inlining could increase executable size
  • Prevents binary compatibility of shared libraries/DLLs
  • Upsets co-workers who prefer the traditional ways of using C++
like image 148
Kristopher Johnson Avatar answered Oct 13 '22 23:10

Kristopher Johnson


Well - one problem is that typically implementations change much more often than class definitions - so for a large project you end up having to recompile the world for every small change.

like image 32
Aaron Avatar answered Oct 13 '22 21:10

Aaron