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

前端 未结 7 1523
野性不改
野性不改 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:54

    In C++, separate compilation of code modules (.c or .cpp files) require the function prototypes to be defined prior to usage. If you want to use classes or methods defined somewhere else, you have to import the .h files to get their definition. At the end, the linker makes sure all promises made in the .h files can be fulfilled by all c/cpp files.

    Also, it allows to created whole frameworks such as boost only by defining .h files.

提交回复
热议问题