tags:

views:

148

answers:

3

What exactly is the point of having multiple source files in c++? Sorry if this incredibly simple.

+7  A: 

In what context do you mean? Multiple headers in a project, or multiple headers included in the same file?

Breaking up code into different files when possible helps to make it much easier to maintain. Instead of having to search through 100,000 lines of code to fine one function definition, you might only have to look at 500. Plus it can speed up recompilation since you can just compile files that have been changed, then link against the previous object files.

For C++ headers in particular, it's generally a good idea to only have one class per header, so you'll have a bunch of different header files. Again, this is much more maintainable.

eldarerathis
Ok, thank you. I was speaking of breaking up classes into multiple headers, I wasnt sure what the proper way to go was and what the benefits were.
Kevin C
@Kevin: In C++, it is not possible to break a class up in multiple portions. The compiler must see the entire `class Name { ... }` in one piece. The only things you can move to a different file (typically a .cpp file) are the bodies of the member functions.
Bart van Ingen Schenau
A: 

I agree. The code is much easier to maintain if the code is broken up into smaller portions per file. I've worked with many projects trying to find issues and it becomes much more difficult when the developer places several classes into one file. I personally prefer to separate each class into an individual file so that when I look at the project I can tell where the class exists. This leaves potential for clutter, but if namespaces are used well then it should be well structured.

dwhittenburg
+1  A: 

Using multiple source files to organize your code can dramatically improve not only compile times but also workflow. It can allow more people to work simultaneously with less chance to step on each others toes.

Breaking classes into multiple header files is not always the best method, and can often lead to confusion and problems compiling the code.

I wouldn't always recommend only one class per header file, but definitely only one header per class.

Tom Slick