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

前端 未结 7 1405
野性不改
野性不改 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.

    0 讨论(0)
  • 2021-01-01 16:49

    Because in most cases, you'll want to use the class somewhere besides the file in which you implement it. If you make the whole program in one file, you don't need the separation.

    You hardly ever want to write a C++ program all in one file.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-01 17:00

    Your edit re: Boost makes an important distinction. Template classes are almost always defined in headers because of the way the compiler and linker work in the current C++ standard. You will find most template libraries (not just Boost) are implemented in header files for the same reason.

    0 讨论(0)
  • 2021-01-01 17:07

    Well, one of the benefits of having the code this way is that it reduces compile time.

    Let's say you have these files on your project:

    • a.h
    • a.cpp
    • b.cpp

    If you already have a.cpp compiled into an object file a.o, then if you include a.h in b.cpp, compilation should be quicker because parser won't have to deal with the whole declaration/definition of a.

    0 讨论(0)
  • 2021-01-01 17:13

    C++ has something called the One Definition Rule. It means that (excluding inline functions), definitions can only appear in one compilation unit. Since C++ header files are just "copy and pasted" at every include file, now you are putting definitions in multiple places if you just put the definitions in header files.

    Of course, you may say, why not make everything inline. Well, if the compiler respects your inline suggestion, code for long functions will be replicated at every call site, making your code excessively large, and possibly causing thrashing, cache issues, and all sorts of unfun stuff.

    On the other hand, if the compiler does not listen to you, and doesn't inline anything, now you have 2 problems: 1) you don't know which translation unit got your classes definitions, and 2) the compiler still has to wade through your definitions every time you #include them. Moreover, there's no easy way to make sure you haven't accidentally defined the same method twice, in 2 different headers, differently.

    You also get a circular dependency issue. For a class to invoke another class's method, that class needs to be declared first. So if 2 classes need to invoke each other's methods, each must be declared before either can be defined. There is no way to do this with declarations and definitions in one file.

    Really, it's how the language and parser were built. It's a pain, but you just have to deal with it.

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