Multiple classes in a header file vs. a single header file per class

The term here is translation unit and you really want to (if possible) have one class per translation unit ie, one class implementation per .cpp file, with a corresponding .h file of the same name.

It’s usually more efficient (from a compile/link) standpoint to do things this way, especially if you’re doing things like incremental link and so forth. The idea being, translation units are isolated such that, when one translation unit changes, you don’t have to rebuild a lot of stuff, as you would have to if you started lumping many abstractions into a single translation unit.

Also you’ll find many errors/diagnostics are reported via file name (“Error in Myclass.cpp, line 22”) and it helps if there’s a one-to-one correspondence between files and classes. (Or I suppose you could call it a 2 to 1 correspondence).

Leave a Comment