Pros & Cons of putting all code in Header files in C++?

前端 未结 17 1903
终归单人心
终归单人心 2020-12-02 15:48

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

17条回答
  •  生来不讨喜
    2020-12-02 15:59

    I disagree with point 1.

    Yes, there is only one .cpp and the built time from scratch is faster. But, you rarely build from scratch. You make small changes, and it would need to recompile the whole project each time.

    I prefer doing it the other way around:

    • keep shared declarations in .h files
    • keep definition for classes that are only used in one place in .cpp files

    So, some of my .cpp files start looking like Java or C# code ;)

    But, 'keeping stuff in .h' approach is good while designing the system, because of point 2. you made. I usually do that while I'm building the class hierarchy and later when code architecture becomes stable, I move code to .cpp files.

提交回复
热议问题