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

后端 未结 6 1236
北荒
北荒 2020-12-03 11:49

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 clas

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 12:06

    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++

提交回复
热议问题