Reputation: 1655
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 or .dll for outside use, what's the point?
Edit: The reason I'm asking is that the Boost libraries put everything in an .hpp file, (most of the libraries anyways), and I wanted to know why it is separated in most of the other code that I see.
Upvotes: 20
Views: 9395
Reputation: 13521
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.
Upvotes: 4
Reputation: 11925
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.
Upvotes: 2
Reputation: 67819
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.
Upvotes: 26
Reputation: 74290
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.
Upvotes: 0
Reputation: 6424
Because even within your DLL other classes will use your class. Those files must see the class declaration at compile time, by including the .h. They must not see the definition or there will be multiple definitions of the class functions.
Upvotes: 2
Reputation: 181350
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:
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.
Upvotes: 6
Reputation: 112376
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.
Upvotes: -1