Coding C++ (mostly) in header files vs .cpp files

后端 未结 1 1391
Happy的楠姐
Happy的楠姐 2021-02-12 19:00

For years I\'ve been coding C++ in the standard way, with class declarations in header files .hpp and the function definitions in source .cpp files. Recently I moved to a new c

相关标签:
1条回答
  • 2021-02-12 19:18

    Off the top of my head:

    Strengths:

    • implementation visible (more of a weakness, but depends on the case)
    • no need to export to a library
    • better chance for the compiler to optimize some of the code

    Weaknesses:

    • implementation visible
    • slower build time
    • bloated header files
    • a change in implementation would require a full rebuild, having the implementation in an implementation file does not (only compile that specific file or library)
    • In case of circular dependencies, you use forward-declarations and only include the full type in the implementation file. If all you have is a header, this is no longer possible.

    I'm sure there are others, I'll edit if I can think of any more.

    0 讨论(0)
提交回复
热议问题