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

后端 未结 6 1243
北荒
北荒 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:22

    Not mentionned yet: virtual functions are instantiated for each include, so you can bloat your executable (I'm not sure whether this is true for all compilers).

    There is an alternative:
    Do a lot of stuff in classes declared in your source-file. 1 example is the pimpl-idiom, but there are also people who are afraid to declare classes out of the header-file. However, this makes sense for private classes.

提交回复
热议问题