Why put a class declaration and definition in two separate files in C++?

前端 未结 7 1552
野性不改
野性不改 2021-01-01 16:16

I\'m just wondering, what is the whole point of separating classes into an .h and a .cpp file? It makes it harder to edit, and if your class won\'t be compiled into a .lib o

7条回答
  •  没有蜡笔的小新
    2021-01-01 16:46

    Boost doesn't inline all of it's code; it inlines template definitions for classes it expects its consumers to instantiate, like shared_ptr. Many libraries have sections that need to be compiled separately, like boost::serialization and program_options.

    Inlining can have severe negative effects as the size of your code base increases. It increases the coupling between your components, not to mention nuking your compile time (which boost does for plenty of other reasons :). Effectively, all of your translational units would have almost a complete copy of the program, and a tiny change would cause you to rebuild/retest everything. On some projects, this could take many, many hours.

    I've never really noticed it being harder to edit; in my experience, it makes it easier because of the clear separation of interface and implementation, and I know which file to go to find what I'm looking for.

提交回复
热议问题