Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why break classes up into multiple source files in C++?

Tags:

c++

file

header

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

like image 656
Kevin C Avatar asked Mar 18 '26 21:03

Kevin C


1 Answers

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.

like image 136
eldarerathis Avatar answered Mar 20 '26 10:03

eldarerathis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!